Some other methods: see LeetCode solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Solution { public: int majorityElement(vector<int>& nums) { unordered_map<int,int> record; // use count() or find() for(int i=0;i<nums.size();i++){ if(record.count(nums[i])) //same with ==> if(record.find(nums[i]!= record.end())) ++record[nums[i]]; else record[nums[i]]=1; if(record[nums[i]] > (nums.size()/2)) return nums[i]; } } }; |
没有评论:
发表评论