169. Majority Element 查找多数元素
2017-12-05 22:15
549 查看
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.
题目解释:查找出现在数组中出现次数多于n/2的,假设数组不为空且一定存在出现次数多于n/2的
class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}
简单想来就是排序之后数组一半处的元素一定是该符合条件的元素。
more than
⌊ n/2 ⌋times.
You may assume that the array is non-empty and the majority element always exist in the array.
题目解释:查找出现在数组中出现次数多于n/2的,假设数组不为空且一定存在出现次数多于n/2的
class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}
简单想来就是排序之后数组一半处的元素一定是该符合条件的元素。
相关文章推荐
- LeetCode 题解 169. Majority Element(查找多数元素)
- LeetCode Problem 169: Majority Element查找多数元素
- [LeetCode]169. Majority Element(多数元素)
- LeetCode-169:Majority Element (数组中的多数元素)
- [LeetCode] 169. Majority Element 多数元素
- 字符串算法——查找数组多数元素(Majority Element II)
- 169. Majority Element (寻找多数元素)
- Leetcode:169. Majority Element(找到数组中出现次数最多的元素)
- 169. Majority Element | 数组中的主元素
- 【LeetCode】169. Majority Element (多数投票算法 & 算法迁移能力)
- leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)
- LeetCode 169. Majority Element (数组的主要元素、摩尔投票算法)
- 【一天一道LeetCode】#169. Majority Element
- LeetCode 169:Majority Element
- 169. Majority Element
- 169. Majority Element
- 169.[LeetCode]Majority Element
- leetCode_Majority Element II【多数投票法】
- ※ Leetcode - Array - 169. Majority Element(快速找出数组中出现过一半以上次数的数,3种写法)
- 169. Majority Element