169. Majority Element
2016-11-25 22:45
197 查看
public static int majorityElement(int[] nums) { if(nums == null || nums.length == 0) { return 0; } int count = 0; int number = nums[0]; for(int i = 0; i < nums.length; i++) { if(number == nums[i]) { count++; }else if(count == 0){ number = nums[i]; count++; }else { count--; } } count = 0; for(int i = 0; i < nums.length; i++) { if(number == nums[i]) { count++; } } if(count >= (nums.length/2)) { return number; }else { return 0; } }
相关文章推荐
- 2018.03.07 leetcode 打卡 #169. Majority Element
- 169. Majority Element
- Leet Code OJ 169. Majority Element [Difficulty: Easy]
- [leetcode] 169. Majority Element
- Leetcode 169 Majority Element
- 169. Majority Element
- 169. Majority Element
- [LeetCode 169]Majority Element: Moore Voting Algorithm
- LeetCode 169. Majority Element
- LeetCode【169. Majority Element】
- leetcode 169. Majority Element
- 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