lintcode/leetcode由易至难第12题:Majority Element
2017-06-08 09:52
423 查看
Problem:
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.
Code:
public class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}
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.
Code:
public class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}
相关文章推荐
- LeetCode : Majority Element
- Leetcode Majority Element
- Leetcode - Majority Element
- LeetCode Majority Element II(Moore Voting Algorithm即Majority Voting Algorithm)
- Leetcode 169 Majority Element
- LeetCode(三)HashMap169. Majority Element&350. Intersection of Two Arrays II
- leetcode MajorityElement相关问题
- Leetcode:169. Majority Element(找到数组中出现次数最多的元素)
- 2018.03.07 leetcode 打卡 #169. Majority Element
- leetcode229-Majority Element II(数组中出现最多的数II)
- leetcode 169. Majority Element 摩尔投票法
- LeetCode 169. Majority Element
- Lintcode 5 Kth Largest Element in an Array
- Lintcode: Majority Number 解题报告
- Leetcode Majority Element
- [Leetcode]#169 Majority Element
- lintcode/leetcode由易至难第14题:Single Number
- LeetCode:Majority Element
- LeetCode Majority Element
- 刷leetcode:Majority Element