leetcode 219: Contains Duplicate II
2015-08-29 00:29
603 查看
Use the unordered map to save the number and its index. And the rest is easy.
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { unordered_map<int,int> mp; int n=nums.size(); for(int i=0;i<n;i++) { if(mp.find(nums[i])!=mp.end()) if(i-mp[nums[i]]<=k) return true; mp[nums[i]]=i; } return false; } };
相关文章推荐
- Gym 100338H High Speed Trains(高精度)
- Reflow & Repaint
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 1) C. CNF 2 无向图找环
- wait、sleep、notify、notifyAll的区别
- 不使用Main.storyboard
- 11 Container With Most Water
- XCode Could not launch "" failed to get the task for process
- leetcode: (219) Contains Duplicate II
- Wait Condition 例子
- 生产者与消费者 & wait()& notify()
- _itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }2
- _itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }
- getpagesize.c:32: __getpagesize: Assertion `_rtld_global_ro._dl_pagesize != 0' failed
- android.intent.action.MAIN与android.intent.category
- vim 配置替换错误E488: Trailing characters
- unity3d 游戏人工智能开发之状态机(C#模板与示例)
- 8.28筆記 Failure getting entry for 0x...、fresco獲取bitmap
- SwingUtilities.invokeLater()和SwingUtilities.invokeAndWait()的区别(有什么区别)
- 自定义属性(styleable,TypeArray,obtainStyledAttributes)
- NSSearchPathForDirectoriesInDomains用法