C++ , map only store two pair


  • 0
    B

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

            for (int i = 0; i<nums.size(); i++) 
            {
                if((int_map.find(nums[i]) )!= int_map.end() ){
                    if ((i - int_map[nums[i]])<= k) {
                        return true;
                    }
                      else
                        int_map.erase(nums[i]);
                }
                int_map.insert(pair<int, int>(nums[i], i));
            }
            return false;
    }
    

    };


  • 0
    X

    Still runs in O(n log n) right?
    Is there an O(n log k) solution?


Log in to reply
 

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