【LeetCode】 Majority Element 系列
2017-06-13 14:46
288 查看
Majority Element 系列
169. 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.
解答
摩尔投票算法
class Solution { public: //摩尔投票算法 int majorityElement(vector<int> & nums) { int count = 1, major = nums[0], n = nums.size(); for(int i = 1; i < n; ++i) { if(!count) major = nums[i],count = 1; else if(nums[i] == major) ++count; else --count; } return major; } };
位操作
class Solution { public: //Bit Manipulation int majorityElement(vector<int> & nums) { int bitCount = 0,majority = 0, n = nums.size(); int mask = 1; while(mask) { int bitCount = 0; for(int j = 0; j < n; ++j) { if(nums[j] & mask) ++bitCount; if(bitCount > n/2) { majority |= mask; break; } } cout << mask << endl; mask <<= 1; } return majority; } };
哈希表
class Solution { public: //哈希表 int majorityElement(vector<int>& nums) { unordered_map<int,int> help; for(auto val : nums) ++help[val]; for(auto val: help) if(val.second > nums.size()/2) return val.first; } }; #### 分治法 ```cpp class Solution { public: //Divide and Conquer 分治法 int majorityElement(vector<int>& nums) { return getMajorityElement(nums,0,nums.size()-1); } private: int getMajorityElement(const vector<int>& nums,int left,int right) { if(left == right) return nums[left]; int mid = (left+right)/2; int leftMajority = getMajorityElement(nums,left,mid); int rightMajority = getMajorityElement(nums,mid+1,right); if(leftMajority == rightMajority) return leftMajority; return count(nums.begin()+left,nums.begin()+mid+1,leftMajority) > count(nums.begin()+mid+1,nums.begin()+right+1,rightMajority) ? leftMajority : rightMajority; } }; <div class="se-preview-section-delimiter"></div>
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.解答
摩尔投票算法
class Solution { public: // Boyer-Moore Majority Vote algorithm 摩尔投票算法 vector<int> majorityElement(vector<int>& nums) { int count1 = 0,count2 = 0; int majority1 = 0, majority2 = 0,n = nums.size(); if(n == 0) return vector<int>(); for(int i = 0; i < n; ++i) { if(nums[i] == majority1) ++count1; else if (nums[i] == majority2) ++count2; else if(count1 == 0) { majority1 = nums[i]; count1 = 1; }else if(count2 == 0) { majority2 = nums[i]; count2 = 1; } else --count1,--count2; } vector<int> res; count1 = count2 = 0; for(auto val : nums) { if(val == majority1) ++count1; if(val == majority2) ++count2; } if(count1 > n/3) res.push_back(majority1); if(majority1 != majority2) { if(count2 > n/3) res.push_back(majority2); } return res; } };
相关文章推荐
- 算法系列(13) Leetcode 169 Majority Element
- Leetcode Majority Element系列 摩尔投票法
- Majority Element I&II | Leetcode
- LeetCode 169: Majority Element
- [LeetCode.Trick]Majority Element||
- 【LeetCode】169. Majority Element
- LeetCode Majority Element(简单题)
- LeetCode 169. Majority Element
- [LeetCode][169][Majority Element]
- leetcode--169. Majority Element
- LeetCode - 169. Majority Element
- LeetCode:Majority Element I II 投票算法
- 【leetcode】Majority Element
- leetcode 169. Majority Element
- Leetcode练习笔记——169. Majority Element
- LeetCode(169)Majority Element
- LeetCode Majority Element
- LeetCode 169 -Majority Element ( JAVA )
- leetcode-Majority Element
- leetcode Majority Element