229. Majority Element II java solutions

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

Hint:

  1. How many majority elements could it possibly have?
  2. Do you have a better hint? Suggest it!

Subscribe to see which companies asked this question

 1 public class Solution {
 2     public List<Integer> majorityElement(int[] nums) {
 3         List<Integer> ans = new ArrayList<Integer>();
 4         if(nums.length <= 0) return ans;
 5         Arrays.sort(nums);
 6         int i = 0,len = nums.length,tmp = 0;
 7         while(i < len - len/3){
 8             if(nums[i] == nums[i+len/3]){
 9                 tmp = nums[i];
10                 ans.add(tmp);
11                 i += len/3;
12                 while(i < len - len/3 && nums[i] == tmp)i++;
13             }else i++;
14         }
15         return ans;
16     }
17 }

解法二:

可用一个hashmap 来计算元素出现个数,但是超过space O(1) 的限制。

解法三:

https://discuss.leetcode.com/topic/32510/java-easy-version-to-understand/2 二刷在研究。

时间: 2024-11-09 02:25:05

229. Majority Element II java solutions的相关文章

169. Majority Element &amp;&amp; 229. Majority Element II

169. Majority Element 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. Hide T

【LeetCode】229. Majority Element II

Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint

LeetCode OJ 229. Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. Hint: How many majority elements could it possibly have? Do you have a better hint? Suggest it! [题目分析]

[LeetCode] 229. Majority Element II 多数元素 II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The algorithm should run in linear time and in O(1) space. Example 1: Input: [3,2,3] Output: [3] Example 2: Input: [1,1,1,3,3,2,2,2] Output: [1,2] 169. Maj

Java for LeetCode 229 Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 解题思路: <编程之美>寻找发帖水王的原题,两次遍历,一次遍历查找可能出现次数超过nums.length/3的数字,(出现三次不同的数即抛弃),第二次验证即可. JAVA实现如下: public Lis

leetcode 229. Majority Element II(多数投票算法)

就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int> majorityElement(vector<int>& nums) { int cnt1=0,cnt2=0,ans1=0,ans2=1; for(auto n:nums){ if(n==ans1){ cnt1++; } else if(n==ans2){ cnt2++; } el

LeetCode 229. Majority Element II (众数之二)

Given an integer array of size n, find all elements that appear more than ? n/3 ? times. The algorithm should run in linear time and in O(1) space. 题目标签:Array 题目给了我们一个 nums array, 让我们找出所有 数量超过 n/3 的众数.这一题与 众数之一 的区别在于,众数之一 只要找到一个 众数大于 n/2 的就可以.这一题要找到所

229. Majority Element II

Given an integer array of size n, find all elements that appear more than ? n/3 ? times. The algorithm should run in linear time and in O(1) space. 用的是摩尔投票法,原版的算法是找一个数组出现次数大于数组长度半数的数(只有当数组中存在这样的数时,这个算法才会生效,而且不难理解,如果有也只可能有一个),方法就是定义一个变量m,记录当前的候选数字,再定义

(medium)LeetCode 229.Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 解法:参考编程之美129页,寻找发帖水王 代码如下: public class Solution { public List<Integer> majorityElement(int[] nums) {