c++ solution.use map.ease understanding


  • 0
    J

    class Solution {
    public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
    map<int, int> mapIndex;
    for (int i = 1; i <= nums.size(); ++i)
    {
    if (mapIndex[nums[i-1]] > 0 )
    if (i - mapIndex[nums[i-1]] <= k) return true;
    mapIndex[nums[i-1]] = i;
    }
    return false;
    }
    };


Log in to reply
 

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