219. Contains Duplicate II 發表於 2023-02-13 | 分類於 leetcode problemsolution1234567891011121314151617class Solution {public: bool containsNearbyDuplicate(vector<int>& nums, int k) { int n = nums.size(); unordered_map<int, int> mp; for(int i=0;i<n;++i){ if(mp.count(nums[i])){ if(( i - mp[nums[i]])<=k) return true; } mp[nums[i]] = i; } return false; }}; analysis time complexity O(nlogn) speed complexity O(n)