Majority Element (算法)

自己写了一种方法,看了别人的解析之后觉得自己的方法好low,不高效也不明智。所以决定记录下来。

题目要求

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

相关TAG

Array   Divide and Conquer   Bit Manipulation

代码

 1 public class Solution {
 2     public int majorityElement(int[] nums) {
 3         //way1:genuis
 4         int major=nums[0], count = 1;
 5         for(int i=1; i<nums.length;i++){
 6             if(count==0){
 7                 count++;
 8                 major=nums[i];
 9             }else if(major==nums[i]){
10                 count++;
11             }else count--;
12         }
13         return major;
14
15         //way2:most efficient
16         int len = nums.length;
17         Arrays.sort(nums);
18         return nums[len/2];
19
20         //way3:mine
21         //n个元素的集合,必须出现(n/2+1)次以上
22         int len = nums.length;
23         if(len==1)
24             return nums[0];
25         Arrays.sort(nums);
26         int majorLen = (len/2)+1;
27         int pointer = 0;
28         int count = 1;
29         while(pointer<len-1){
30             if(count>=majorLen)
31               break;
32             else{
33                 if((pointer+1<len)&&(nums[pointer]==nums[pointer+1]))
34                     count++;
35                 else
36                     count=1;
37                 pointer++;
38             }
39         }
40         return nums[pointer];
41     }
42 }
时间: 2024-08-05 20:26:59

Majority Element (算法)的相关文章

leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)

题目: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 题解:运用多数投票算法的思路来解:从头到尾遍历数

【算法31】寻找数组的主元素(Majority Element)

题外话 最近有些网友来信问我博客怎么不更新了,是不是不刷题了,真是惭愧啊,题还是在刷的,不过刷题的频率没以前高了,看完<算法导论>后感觉网上很多讨论的题目其实在导论中都已经有非常好的算法以及数学证明,只是照搬的话好像意义也不是很大,希望找到些有代表性的题目在更新,另外希望能接着前面的<穷举递归和回溯算法终结篇>一系列如动态规划.贪心算法类的终结篇,在梳理自己知识结构的同时也能够帮助读者们更系统的学习算法思想.好了话不多说,进入正题. 问题描述 给定一个数组A[n], 定义数组的主元

leetcode[169] Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素. 我想到的方法是 1. 排序,然后扫描一次就知道了.总共nlgn 2. 哈希,记录每个次数,O(n)的时间和空间. class Solution { public: int majorityElement(vector<int> &num) { unordered_map<int, int> umap; for (int i = 0; i < num.size(); i++) { umap[num[i]

LeetCode——Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element.但是还有两种更有意思的实现方式时间效率O(n),空间效率O(1):1.Moore voting algorithm 投票算法,因为符合要求的majority element总是存在的,所以首先置计数器count=1,并选择数组的第一个元素作为candidate,往后遍历并计数,与candidate相

LeetCode OJ 229. Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint? Suggest it! [题目分析]

leetcode169——Majority Element (C++)

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 个人博客:http://www.cnblogs.com/

leetcode笔记:Majority Element

一. 题目描写叙述 Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. 二. 题目分析 题目说到,给定一个数

LeetCode 4 :Majority Element

problem:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. problem analysis: 1.首先,需要统计数组元素出现的次数,包括不同的元素有几个?每一个不同的元素出现了几次?同时需要将不同的元素及出现的频率正确对应. 2.在第一步完成之后,可以寻找最大的频率数,然后找到major

LeetCode169:Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. Credits: Special thanks to @