169 Majority Element
2014-12-25 08:42
295 查看
public class Solution {
public int majorityElement(int[] num) {
System.out.println(num.length);
int major = num[0];
int count = 1;
for(int i = 1;i < num.length ; i++){
if(num[i] == major){
count++;
}else{
count--;
if(count == 0){
if(i+1 < num.length){
major = num[i+1];
count=1;
i++;
}
}
}
}
return major;
}
}
public int majorityElement(int[] num) {
System.out.println(num.length);
int major = num[0];
int count = 1;
for(int i = 1;i < num.length ; i++){
if(num[i] == major){
count++;
}else{
count--;
if(count == 0){
if(i+1 < num.length){
major = num[i+1];
count=1;
i++;
}
}
}
}
return major;
}
}
相关文章推荐
- 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
- 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
- 169. Majority Element