169. Majority Element
2016-03-27 19:33
232 查看
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.
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 = 0; int majority = nums[0]; for(int i = 0;i<nums.size();++i) { if(count == 0) { ++count; majority = nums[i]; } else if(nums[i] == majority) { ++count; } else { --count; } } return majority; } };
相关文章推荐
- JavaScript作用域和变量提升
- MATLAB中利用多幅图片生成GIF动画
- 关于数组的map、reduce、filter
- IOS之Objective-C学习 策略模式
- 20145232 韩文浩 《Java程序设计》第4周学习总结
- 最大子数列之和问题
- 没有括号的sizeof,sizeof为关键字
- 春天来了
- vim配置
- poj--2549--Sumsets(二分)
- 63. Unique Paths II
- linux使用小记
- codeforces 118D Caesar's Legions (好题)
- jian
- Http协议与TCP协议简单理解
- Android之百度定位
- 区间相关问题解法
- hdu 5652 India and China Origins 二分+bfs
- 编写一个简单的配置文件
- 2014 ACM/ICPC 鞍山赛区现场赛 D&I 解题报告