C++ solution use map


  • 0
    J
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
            unordered_map<int,int> M;
            int mark = 0;
            for(int i = 0 ;i < nums.size(); i++){
                if(M.find(nums[i]) == M.end())
                    M[nums[i]] = i;
                else{
                    mark = M[nums[i]];   //mark is old index, M[nums[i]] is the latest index
                    M[nums[i]] = i;
                    if(i-mark <= k)
                        return true;
                }
            }
            return false;
        }
        
       //  short version
        class Solution {
        public:
            bool containsNearbyDuplicate(vector<int>& nums, int k) {
                unordered_map<int,int> M;
                for(int i = 0 ;i < nums.size(); i++){
                    if((M.find(nums[i]) != M.end())&&(i-M[nums[i]] <= k))
                        return true;
                    M[nums[i]] = i;
                }
                return false;
            }
        };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.