[LeetCode]Contains Duplicate II
2015-05-29 12:35
447 查看
Given an array of integers and an integer k, return true if and only if 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 Source]
hash map
nums[i] = nums[j] and the difference between i and j is at most k.
[LeetCode Source]
hash map
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { unordered_map<int,int> temp; for(int i=0;i<nums.size();++i){ if(temp.find(nums[i])==temp.end()){ temp[nums[i]] = i; } else{ if(k>=(i-temp[nums[i]])) return true; } } return false; } };
相关文章推荐
- Minimum Average Waiting Time
- make_pair
- golang 的 sync.WaitGroup
- 捕获fail2ban截获的Ip写入拒绝主机列表
- ArtifactTransferException: Failure to transfer xalan:xalan:jar:2.6.0 from http://repo.maven.apache.o
- AIX telnet: connect: Cannot reach the destination network.
- DNA repair问题
- SSH远程连接报错:Could not connect to '10.43.2.113' (port 22): Connection failed.
- leetcode--Contains Duplicate
- Postfix maillog邮件发送各阶段延时的日志记录
- JetBrain WebStorm 注册码
- naive bayes
- To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
- hdu1151 Air Raid
- Song -----See You Again
- 【转载】failed to create virtual machine
- [翻译] JHChainableAnimations
- [LeetCode] Contains Duplicate 包含重复值
- retain和release倒底怎么玩?
- Factorial Trailing Zeroes