剑指offer 数组中出现次数超过一半的数字
2018-03-27 11:07
274 查看
class Solution { public: int MoreThanHalfNum_Solution(vector<int> numbers) { int n = numbers.size(); if (n == 0) return 0; int num = numbers[0], count = 1; for (int i = 1; i < n; i++) { if (numbers[i] == num) count++; else count--; if (count == 0) { num = numbers[i]; count = 1; } } // Verifying count = 0; for (int i = 0; i < n; i++) { if (numbers[i] == num) count++; } if (count * 2 > n) return num; return 0; } };
相关文章推荐
- 剑指offer:数组中出现次数超过一半的数字
- 剑指offer-数组中出现次数超过一半的数字-php
- (剑指offer)数组中出现次数超过一半的数字
- 《剑指offer》面试题29:数组中出现次数超过一半的数字
- 《剑指offer》数组中出现次数超过一半的数字
- 数组中出现次数超过一半的数字(剑指offer)
- 剑指offer:数组中出现次数超过一半的数字
- 剑指offer 数组中出现次数超过一半的数字
- 剑指offer—数组中出现次数超过一半的数字
- 剑指offer第二十八题【数组中出现次数超过一半的数字】c++实现
- 剑指offer--数组中出现次数超过一半的数字
- 剑指offer系列之27:数组中出现次数超过一半的数字
- 剑指offer(二十四)之数组中出现次数超过一半的数字
- 剑指offer - 数组中出现次数超过一半的数字
- 剑指offer面试题 求数组中出现次数超过一半的数字
- 剑指offer(30):数组中出现次数超过一半的数字
- 《剑指offer》——数组中出现次数超过一半的数字
- 剑指offer——面试题29:数组中出现次数超过一半的数字
- 《剑指offer》-数组中出现次数超过一半的数字
- 剑指Offer面试题29:数组中出现次数超过一半的数字 Java实现