leetcode Majority Element
来源:互联网 发布:java string split n 编辑:程序博客网 时间:2024/06/05 12:48
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.
思路,
这个数字出现的次数,比其他所有元素的总和都要多。那就可以这样:
定义两个变量,一个记录当前major的出现次数,一个记录major。遍历时,若是两数相同,则次数加一。否则减1.若次数小于0,则替换当前的major.
int majorityElement(int* nums, int numsSize) { int major=nums[0]; int flag=1; for(int i=1;i<numsSize;i++) { if(nums[i]!=major) flag--; else flag++; if(flag<0){ major = nums[i]; flag=0; } } return major;}
0 0
- 【leetcode】Majority Element II,Majority element algorithm
- Leetcode: Majority Element &Majority Element II
- Leetcode Majority Element
- leetcode 169-Majority Element
- 【leetcode 哈希表】Majority Element
- LeetCode:Majority Element
- leetcode:Majority Element
- leetcode: Majority Element
- [LeetCode] Majority Element
- leetcode 153: Majority Element
- leetcode Majority Element
- LeetCode:Majority Element
- leetcode:Majority Element
- [LeetCode] Majority Element
- Leetcode-Majority Element
- [leetcode]Majority Element
- [LeetCode]Majority Element
- Leetcode: Majority Element
- android中的<uses-feature>以及<uses-permission>
- idea 快捷键
- 随机梯度下降,批量梯度下降,牛顿法,拟牛顿法
- 怎样正确设置remote_addr和x_forwarded_for
- angularJS学习资源
- leetcode Majority Element
- 把字符串转换成整数
- 畅通工程续
- 160727
- Unity-屏幕适配UGUI与3D/2D场景
- yii 校验登录(场景)
- HTML5学习(三)—2
- 通过经纬度获取地名
- 光纤激光端面泵浦或者侧面泵浦