32ms c++ solution using hashset and queue


  • 0
    Y
         if(nums.empty()) return false;
         unordered_set<int> mySet;   
         queue<int> temp;
         for(int i=0;i <= nums.size()-1;++i){
             while(mySet.size() < k+1 && (i <= nums.size()-1)){
                     if(mySet.find(nums[i]) == mySet.end()){
                     mySet.insert(nums[i]);
                     temp.push(nums[i]);
                     ++i;
                 }
                 else return true;
             }
             if(mySet.size() == k+1 && (i <= nums.size()-1)){
                 mySet.erase(temp.front());
                 temp.pop();
                 if(mySet.find(nums[i]) == mySet.end()){
                     mySet.insert(nums[i]);
                     temp.push(nums[i]);
                 }
                 else return true;
             }    
         }
         return false;

Log in to reply
 

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