c++ solution with map easy solutin


  • 0
    H

    bool containsNearbyDuplicate(vector<int>& nums, int k) {
    map<int,int> mp;
    map<int,int> ::iterator iter;
    mp.clear();

    	for(int i = 0; i < nums.size(); i++){
    		iter = mp.find(nums[i]);
    		
    		if(iter != mp.end()){
    			int pos = iter->second;
    			if(abs(pos-i) <= k) {
    				return true;
    			} else{
    				mp[nums[i]] = i;
    			}
    		} else{
    			mp[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.