169、Majority Element
2015-01-27 16:20
204 查看
Given an array of size n, find the majority element. The majority element is the element that appears more than
You may assume that the array is non-empty and the majority element always exist in the array.
Divide and Conquer Array Bit
Manipulation
里面居然涉及到比特位操作,不明白此题本意到底是要寻求怎样一个解法
⌊ n/2 ⌋times.
You may assume that the array is non-empty and the majority element always exist in the array.
Divide and Conquer Array Bit
Manipulation
里面居然涉及到比特位操作,不明白此题本意到底是要寻求怎样一个解法
int majorityElement(vector<int> &num) { //这个题目也在PAT题里面看到过,求占有量超过一半的数字 int count = 1; int value = num[0]; int len = num.size(); for (int i = 1; i < len; ++i) { if (num[i] == value) ++count; else --count; if (count < 0) { value = num[i]; count = 1; } } return value; }
相关文章推荐
- 2018.03.07 leetcode 打卡 #169. Majority Element
- [LeetCode]169. Majority Element
- 【leetcode】【169】Majority Element
- 169. Majority Element
- 【Leetcode】:169. Majority Element 问题 in Go语言
- Leetcode 169. Majority Element
- 169. Majority Element
- LeetCode 169 Majority Element
- 189. Rotate Array \ 169. Majority Element \ Best time to buy
- 第六周:[leetcode] 多思路解 169.Majority Element
- LeetCode:169. Majority Element
- [LeetCode] 169. Majority Element
- Leetcode#169 Majority Element
- OJ 169 Majority Element
- LeetCode 169. Majority Element and LeetCode 229. Majority Element II解题报告
- LeetCode 169. Majority Element
- Leetcode:169. Majority Element
- [LeetCode] Algorithms-169. Majority Element
- 169. Majority Element
- 15算法课程 169. Majority Element