961 LeetCode 重复N次的元素

题目描述:
在这里插入图片描述
思路:
首先使用哈希表记录每个数字出现次数
然后找到与N相等的次数的数字

代码如下:

class Solution {
public:
    int repeatedNTimes(vector<int>& A) {
        int N=A.size()/2;
        map<int,int>cnt;
        for(int i=0;i<A.size();i++){
            cnt[A[i]]++;
        }
        for(int i=0;i<A.size();i++){
            if(cnt[A[i]]==N)
            return A[i];
        }
        return 0;
    }
};
发布了158 篇原创文章 · 获赞 0 · 访问量 1615

猜你喜欢

转载自blog.csdn.net/peachzy/article/details/104609443
今日推荐