๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Leetcode

[Leetcode,c++] Majority Element

by dkswnkk 2021. 11. 28.

๋ฌธ์ œ

 

Majority Element - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

์ฝ”๋“œ

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

๋Œ“๊ธ€