Why my code can not pass the test in Leetcode while it pass in my VC project?


  • 0
    Z

    I use unordered_map, it can't pass the test case: {1,2,1} k=1. But in my vc project there is no problem. Why??

    class Solution {
    public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {

    	unordered_map<int, int> tmp;
    
    	//check the input
    	if (k == 0)return false;
    	for (int i = 0; i<nums.size(); i++){
    		if (i<k + 1){
    			tmp[nums[i]] = i;
    			if (tmp.size()<i + 1)return true;
    		}
    		else{
                tmp.erase(tmp.begin());
    			tmp[nums[i]] = i;
    			
    			if (tmp.size()<k + 1)return true;
    		}
    	}
    	return false;
    }
    

    };


Log in to reply
 

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