169. Majority Element
題目原文
Input: [3,2,3]
Output: 3Input: [2,2,1,1,1,2,2]
Output: 2解題思路
程式解答
class Solution
{
public:
int majorityElement(vector<int>& nums)
{
int n = nums.size() / 2;
map<int, int> p;
for (auto i : nums)
{
p[i]++;
if (p[i] > n)
return i;
}
}
};Last updated