LeetCode:Contains Duplicate II
2015-06-08 11:02
369 查看
Problems:
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 i and j is at most k.
解法一:leetcode的测试用例不全
解法一:hashmap用来记录已出现的索引数
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 i and j is at most k.
解法一:leetcode的测试用例不全
解法一:hashmap用来记录已出现的索引数
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { if(nums.size()==0|| k==0) return false; unordered_map<int,int> maping; for(int i=0;i<nums.size();i++) { if(maping.find(nums[i])!=maping.end() && i-maping[nums[i]]<=k) return true; else maping[nums[i]]=i; } return false; } };
相关文章推荐
- No space left on device svn: Commit failed (details follow): svn: Can't flush file '/srv/tsocsvn/ts
- Train Problem II
- AIX UNIX 系统管理、维护与高可用集群建设——数据库结构设计
- LeetCode:Contains Duplicate
- ROOT.SH OR ACFSROOT INSTALL, FAILS: ACFS-9109: SLES11 SP3
- Climbing Stairs
- 用opencv自带的traincascade.exe训练给予haar特征和LBP特征的分类器
- ssh: Could not resolve hostname you: Temporary failure in name resolution
- Contains Duplicate II ——LeetCode
- 丸美集团TurboMail邮件系统最新上线
- HDU 1023 Train Problem II (卡特兰数,经典)
- POJ - 1422 Air Raid 二分图最大匹配
- enable usb tethering via command line failed
- poj 3691 DNA repair(AC自动机+dp)
- [leetcode] Climbing Stairs
- int main(int argc,char* argv[])详解
- POJ 1273 -Drainage Ditches
- LeetCode --- Climbing Stairs
- HDOJ 1787 GCD Again(欧拉函数)
- leetCode 之 Contains Duplicate