Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and jis at most k.
//給出一個整數數組,判斷該數組內是否有兩個元素值是相同的,且他們的索引值相差不大於k,是則返回true,否則返回false class Solution { public: bool containsNearbyDuplicate(vector& nums, int k) { map buf; int n = nums.size(); for (int i = 0; i < n; i++) { if (buf.find(nums[i]) != buf.end() && i - buf[nums[i]] <= k) { return true; } else { buf[nums[i]] = i; } } return false; } };