LeetCode169. Majority Elementapp
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times. (Easy)spa
You may assume that the array is non-empty and the majority element always exist in the array.code
分析:blog
抵消的思想,維護一個result和count,與result相同則count++,不然count--,到0時更新result的值,因爲主元素個數較多,全部最後必定能留下。element
代碼:hash
1 class Solution { 2 public: 3 int majorityElement(vector<int>& nums) { 4 int result = 0; 5 int count = 0; 6 for (int i = 0; i < nums.size(); ++i) { 7 if (nums[i] == result && count != 0) { 8 count++; 9 } 10 else if (count == 0) { 11 result = nums[i]; 12 count = 1; 13 } 14 else { 15 count--; 16 } 17 } 18 return result; 19 } 20 };
LintCode47. Majority NumberIIit
Given an array of integers, the majority number is the number that occurs more than 1/3
of the size of the array. (Medium)io
Find it.class
Notice : There is only one majority number in the array. hashmap
分析:
同majority number1的思路類似,維護兩個result和count,相同則對相應count操做,不一樣則均減一;
注意最後剩下兩個元素,並不必定count值大的就必定是出現次數多的(可能另外一個參與抵消過多),因此須要從新遍歷一遍,對這個兩個數比較出現次數大小。
代碼:
1 class Solution { 2 public: 3 /** 4 * @param nums: A list of integers 5 * @return: The majority number occurs more than 1/3. 6 */ 7 int majorityNumber(vector<int> nums) { 8 // write your code here 9 int candidate1 = 0, candidate2 = 0; 10 int count1 = 0, count2 = 0; 11 for (int i = 0; i < nums.size(); ++i) { 12 if (nums[i] == candidate1 && count1 != 0) { 13 count1++; 14 } 15 else if (nums[i] == candidate2 && count2 != 0) { 16 count2++; 17 } 18 else if (count1 == 0) { 19 candidate1 = nums[i]; 20 count1 = 1; 21 } 22 else if (count2 == 0) { 23 candidate2 = nums[i]; 24 count2 = 1; 25 } 26 else { 27 count1--; 28 count2--; 29 } 30 } 31 count1 = 0; 32 count2 = 0; 33 for (int i = 0; i < nums.size(); ++i) { 34 if (nums[i] == candidate1) { 35 count1++; 36 } 37 else if (nums[i] == candidate2) { 38 count2++; 39 } 40 } 41 return count1 > count2 ? candidate1 : candidate2; 42 } 43 };
LeetCode229. 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.(Medium)
分析:
這個與lintcode中的majority number2基本類似,只是要求找到全部的大於n / 3次的元素(至多也就是兩個);
因此最後一步從比較兩個canditate的count大小,變成將這兩個count與 size() / 3比較。
代碼:
1 class Solution { 2 public: 3 vector<int> majorityElement(vector<int>& nums) { 4 int candidate1 = 0, candidate2 = 0; 5 int count1 = 0, count2 = 0; 6 for (int i = 0; i < nums.size(); ++i) { 7 if (nums[i] == candidate1 && count1 != 0) { 8 count1++; 9 } 10 else if (nums[i] == candidate2 && count2 != 0) { 11 count2++; 12 } 13 else if (count1 == 0) { 14 candidate1 = nums[i]; 15 count1 = 1; 16 } 17 else if (count2 == 0) { 18 candidate2 = nums[i]; 19 count2 = 1; 20 } 21 else { 22 count1--; 23 count2--; 24 } 25 } 26 count1 = 0; 27 count2 = 0; 28 for (int i = 0; i < nums.size(); ++i) { 29 if (nums[i] == candidate1) { 30 count1++; 31 } 32 else if (nums[i] == candidate2) { 33 count2++; 34 } 35 } 36 vector<int> result; 37 if (count1 > nums.size() / 3) { 38 result.push_back(candidate1); 39 } 40 if (count2 > nums.size() / 3) { 41 result.push_back(candidate2); 42 } 43 return result; 44 45 } 46 };
LintCode48. Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1/k
of the size of the array. (Medium)
Find it.
Notice:There is only one majority number in the array.
分析:
從前一題的1/3變爲1/k,道理仍是同樣,不過此次須要用一個hashmap來維護出現的次數,注意unordered_map插入刪除相關操做的寫法便可。
尤爲hashmap元素個數等於k須要刪除的時候,須要維護一個vector存key,若是用iterator邊走邊刪除可能出現位置變化。
代碼:
1 class Solution { 2 public: 3 /** 4 * @param nums: A list of integers 5 * @param k: As described 6 * @return: The majority number 7 */ 8 int majorityNumber(vector<int> nums, int k) { 9 // write your code here 10 unordered_map<int, int> hash; 11 for (int i = 0; i < nums.size(); ++i) { 12 if (hash.size() < k) { 13 hash[nums[i]]++; 14 } 15 else { 16 vector<int> eraseVec; 17 for (auto itr = hash.begin(); itr != hash.end(); ++itr) { 18 (itr -> second)--; 19 if (itr -> second == 0) { 20 eraseVec.push_back(itr -> first); 21 } 22 } 23 for (int i = 0; i < eraseVec.size(); ++i) { 24 hash.erase(eraseVec[i]); 25 } 26 hash[nums[i]]++; 27 } 28 } 29 for (auto& n : hash) { 30 n.second = 0; 31 } 32 for (int i = 0; i < nums.size(); ++i) { 33 if (hash.find(nums[i]) != hash.end()) { 34 hash[nums[i]]++; 35 if (hash[nums[i]] > nums.size() / k) { 36 return nums[i]; 37 } 38 } 39 } 40 return -1; 41 } 42 };