【LeetCode】219. Contains Duplicate II
2015-07-07 20:08
411 查看
Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.
使用unordered_map记录每个值与对应index位置,当出现重复值时,比较index距离是否在k之内,
注意index的更新
Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.
使用unordered_map记录每个值与对应index位置,当出现重复值时,比较index距离是否在k之内,
注意index的更新
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { unordered_map<int, int> m; // value --> index for(int i = 0; i < nums.size(); i ++) { if(m.find(nums[i]) == m.end()) m[nums[i]] = i; else { if(i - m[nums[i]] <= k) return true; else m[nums[i]] = i; // update the index } } return false; } };
相关文章推荐
- Contains Duplicate II
- 实战weblogic集群之创建domain,AdminServer
- Contains Duplicate
- INSTALL_PARSE_FAILED_NO_CERTIFICATES 解决办法
- 【LeetCode】217. Contains Duplicate (2 solutions)
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法
- 进程控制 [fork() exec() wait() waitpid()]
- AIX Study之--AIX网卡配置管理(ent0、en0、et0)
- Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or d.......
- Main.storyboard
- AIX Study之--AIX网卡配置管理(ent0、en0、et0)
- 关于[[NSBundle mainBundle] pathForResource:@"name" ofType:@"type"]找不到指定文件的解决办法
- LeetCode Container With Most Water
- WSAIoctl
- AIX Study之--用户访问系统资源限制配置
- AIX Study之--AIX用户资源访问限制配置
- Grails边做边学入门篇[0]--------讲讲什么是Grails
- Git Pull Failed: CONFLICT (content): Merge conflict in camus-aggregator/camus-admin-web/src/main/web
- Tuning TIME_WAIT
- 责任链模式 Chain Of Responsibility