๋ฌธ์
์ฝ๋
class Solution {
public:
int majorityElement(vector<int>& nums) {
map<int,int>m;
for(int i:nums) m[i]++;
int cnt=-1,ans;
for(auto it=m.begin(); it!=m.end(); it++){
if(it->second>cnt){
cnt=it->second;
ans=it->first;
};
}
return ans;
}
};
ํ์ด
๊ฐ์ฅ ๋ง์ ์ซ์๋ฅผ ์ถ๋ ฅํ๋ฉด ๋ฉ๋๋ค. ์ ๋ map์ value๊ฐ์ผ๋ก ๋น๋์๋ฅผ ํ์ ํด์ ํ์ดํ์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leetcode,c++] Add Digits (0) | 2022.03.02 |
---|---|
[Leetcode,c++] Happy Number (0) | 2022.03.02 |
[Leetcode,c++] Network Delay Time (0) | 2021.11.22 |
[Leetcode,c++] Valid Anagram (0) | 2021.11.20 |
[Leetcode,c++] Single Number (0) | 2021.11.19 |
[Leetcode,c++] Combination Sum II (0) | 2021.11.16 |
๋๊ธ