LeetCode OJ 之 Contains Duplicate II (包含重复判断- 二)
2015-06-01 16:09
441 查看
题目:
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.
给定整型数组和整数k,查找是否存在两个相等的数它们的下标之差 <= k。
思路:
使用map保存值和位置。代码:
class Solution {public:
bool containsNearbyDuplicate(vector<int>& nums, int k)
{
unordered_map<int , int> map;//键是nums[i],值是i
for(int i = 0 ; i < nums.size(); i++)
{
if(map.find(nums[i]) != map.end() && (i - map[nums[i]] <= k))
return true;
map[nums[i]] = i;//如果已经存在nums[i],则会取代之前的值
}
return false;
}
};
相关文章推荐
- LeetCode OJ 之 Contains Duplicate (包含重复判断)
- cocoa pod 制作私有静态库时写完codspec文件又遇到了linker command failed却没有具体错误原因的问题
- [LeetCode] Contains Duplicate III
- socket domain 样例
- org.hibernate.AssertionFailure: null id don't flus
- socketpair初识
- LeetCode Container With Most Water
- http://blog.csdn.net/liuhe688/article/details/6715983/
- YT02-简单数学课堂题-1004 Fibonacci Again -(5.31日-烟台大学ACM预备队解题报告)
- Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的权利字符串" UserInfo=0x1bad30 {NSLocalizedDescription=未找到应用程序的“aps-environment”的权利字符串}
- Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER 解决方案
- HackerRank - "Detecting Valid Latitude and Longitude Pairs"
- http error 503. the service is unavailable. sharepoint
- Leetcode Problem.172—Factorial Trailing Zeroes
- AIX下的nmon的使用
- 在可以调用OLE之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute
- shrio 权限管理filterChainDefinitions过滤器配置
- httpd: Could not reliably determine the server's fully qualified domain name
- leetcode_Contains Duplicate II_easy
- EAI和ESB