169. Majority Element (寻找多数元素)
2016-10-27 23:14
531 查看
Given an array of size n, find the majority element. The majority element is the element that appears more than
You may assume that the array is non-empty and the majority element always exist in the array.
public class Solution {
public int majorityElement(int[] nums) {
int temp=nums[0],times=0;
for(int i:nums){
if(times==0){
temp = i;
}
if(temp==i)
times++;
else{
times--;
}
}
return temp;
}
}
⌊ n/2 ⌋times.
You may assume that the array is non-empty and the majority element always exist in the array.
public class Solution {
public int majorityElement(int[] nums) {
int temp=nums[0],times=0;
for(int i:nums){
if(times==0){
temp = i;
}
if(temp==i)
times++;
else{
times--;
}
}
return temp;
}
}
相关文章推荐
- LeetCode-169:Majority Element (数组中的多数元素)
- 169. Majority Element 查找多数元素
- LeetCode 题解 169. Majority Element(查找多数元素)
- [LeetCode] 169. Majority Element 多数元素
- [LeetCode]169. Majority Element(多数元素)
- LeetCode Problem 169: Majority Element查找多数元素
- Leetcode:169. Majority Element(找到数组中出现次数最多的元素)
- leetcode Majority Element 寻找出现次数最多元素
- 169. Majority Element--寻找数组中出现次数超过一半的数据,229. Majority Element II--注意最后的检测
- 【LeetCode】169. Majority Element (多数投票算法 & 算法迁移能力)
- [LeetCode]169. Majority Element寻找数组中超过一半的那个数
- 字符串算法——查找数组多数元素(Majority Element II)
- leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)
- 寻找多数元素(Majority)
- leetcode-169. Majority Element 寻找众数,hash方法
- 169. Majority Element | 数组中的主元素
- LeetCode 169. Majority Element (数组的主要元素、摩尔投票算法)
- Leetcode 169. Majority Element
- Leetcode169: Majority Element
- LeetCode 169 --- Majority Element