Sword Finger Offer Interview Question 03. Repeated numbers in the array [Simple]

My solution:

Use map to traverse the array and return when the element has been saved in the map

class Solution {
public:
    int findRepeatNumber(vector<int>& nums) {
        map<int,int> m;
        for(int i=0;i<nums.size();i++){
            if(m.count(nums[i])) return nums[i];
            m[nums[i]]=1;
        }
        return -1;
    }
};

Published 65 original articles · Like1 · Visits 493

Guess you like

Origin blog.csdn.net/qq_41041762/article/details/105433536
Recommended