LeetCode 219. Contains Duplicate II
2016-05-04 22:14
387 查看
算法菜鸟今天开始正式刷LeetCode希望能坚持下去
题目:Given an array of integers and an integer k,
find out whether there are two distinct indices i and j in
the array such that nums[i] = nums[j] and
the difference between i and jis
at most k.
难度:Easy
总结:
主要熟悉map的基本用法
map的定义 std::map<int,int> data
map插入数据 map[10]="hello"
map的迭代器std::map<int,int>::iterator it
使用两个for循环的话会超时
Runtime: 72
ms
AC代码
题目:Given an array of integers and an integer k,
find out whether there are two distinct indices i and j in
the array such that nums[i] = nums[j] and
the difference between i and jis
at most k.
难度:Easy
总结:
主要熟悉map的基本用法
map的定义 std::map<int,int> data
map插入数据 map[10]="hello"
map的迭代器std::map<int,int>::iterator it
使用两个for循环的话会超时
Runtime: 72
ms
AC代码
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { bool iscontain=false; std::map<int,int> data; std::map<int,int>::iterator it; for(int i=0;i<nums.size();i++) { it=data.find(nums[i]); if(it==data.end()) {data[nums[i]]=i;} else{ int j=data[nums[i]]; if((i-j)<=k){ iscontain=true; } else{ data[nums[i]]=i; } } } return iscontain; } };
相关文章推荐
- Leetcode - Contains Duplicate II
- Leetcode - Contains Duplicate
- TensorFlow教程04:针对机器学习初学者的MNIST实验——源码和运行结果
- ERROR<53761> - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...
- HDU 1022 Train Problem I
- (Leetcode)11. Container With Most Water
- 借助Clarifai的深度学习 电脑也能看懂视频了
- TensorFlow教程03:针对机器学习初学者的MNIST实验——回归的实现、训练和模型评估
- SCVMM-01 Create Highly Available Virtual Machines
- int i=1; main() {int i=i;}有什么错?
- Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot
- Rails--default_scope
- 子类用一个名为subitems的数组属性保存父类对象,为什么不能获取父类对象的信息呢???在main函数中subitems为空,不知道为什么。
- 启动hadoop报No Route to Host from lida1/10.30.12.87 to lida3:8485 failed on socket timeout exception:
- lightoj 1282 Leading and Trailing
- http://blog.csdn.net/jiyiqinlovexx/article/details/51173080
- 解决httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0
- No cache or cacheManager properties have been set. Authorization cache cannot be obtained.
- ld: library not found for -lPods-socialShare clang: error: linker command failed with exit code 1 报错
- 虚拟机已死,容器才是未来?——VM + Container