Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋
times.javascript
Note: The algorithm should run in linear time and in O(1) space.html
Example 1:java
Input: [3,2,3] Output: [3]
Example 2:數組
Input: [1,1,1,3,3,2,2,2] Output: [1,2]
找到數組中全部出現次數大於⌊ n/3 ⌋
的元素。app
限制時間爲O(N)、空間爲O(1),所以不能用Hash或者排序。使用 169. Majority Element (E) 中提到的摩爾投票法 Boyer-Moore Majority Vote。求全部出現次數大於⌊ n/3 ⌋
的元素,用反證法很容易證實這種元素最多隻有兩個,因此能夠先用摩爾投票法得到兩個候選元素,再從新遍歷數組統計候選元素出現的次數,將知足條件的加入到結果集中。spa
class Solution { public List<Integer> majorityElement(int[] nums) { List<Integer> ans = new ArrayList<>(); int a = 0, b = 0; int countA = 0, countB = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] == a) { countA++; } else if (nums[i] == b) { countB++; } else if (countA == 0) { a = nums[i]; countA = 1; } else if (countB == 0) { b = nums[i]; countB = 1; } else { countA--; countB--; } } countA = 0; countB = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] == a) { countA++; } else if (nums[i] == b) { countB++; } } if (countA > nums.length / 3) { ans.add(a); } if (countB > nums.length / 3) { ans.add(b); } return ans; } }
/** * @param {number[]} nums * @return {number[]} */ var majorityElement = function (nums) { let ans = [] let a = 0, b = 0, countA = 0, countB = 0 for (let num of nums) { if (num === a) { countA++ } else if (num === b) { countB++ } else if (countA === 0) { a = num countA = 1 } else if (countB === 0) { b = num countB = 1 } else { countA-- countB-- } } countA = 0, countB = 0 for (let num of nums) { if (num === a) { countA++ } else if (num === b) { countB++ } } if (countA > Math.trunc(nums.length / 3)) ans.push(a) if (countB > Math.trunc(nums.length / 3)) ans.push(b) return ans }