[leetcode]Contains Duplicate II
2015-06-12 16:03
337 查看
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.
题意:给出一组整数,从中找出是否存在下标i,j之间使得nums[i]=nums[j]并且两个下标之间存在至多k个不同元素。
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { map<int, vector<int> > mivi; for(int i = 0; i < nums.size(); i++){ mivi[nums[i]].push_back(i); if(mivi[nums[i]].size() > 1){ vector<int> v = mivi[nums[i]]; if(v[v.size() - 1] - v[v.size() - 2] <= k) return true; } } return false; } };
相关文章推荐
- [leetcode]Contains Duplicate
- 安装apk时出现错误Failure [INSTALL_FAILED_DEXOPT]问题解决的方法
- 通过AIDL实现挂断电话功能
- 人工智能领域中声源定位的研究与发展------第二章 声源定位系统 (3)
- 不懂技术的人请不要对懂技术的人说这很容易
- 打包contrail-setup
- LeetCode 11 Container With Most Water
- $GetDomesticAirlinesTimeResult_type0@2bb5340c类错误
- HDOJ 1021 Fibonacci Again
- 十大经典数据挖掘算法(9) 朴素贝叶斯分类器 Naive Bayes
- leetcode之Contains Duplicate
- 大麦回忆录-maizuo开发
- Handler sendMessage 与 obtainMessage (sendToTarget)比较
- Leetcode220-Contains Duplicate III
- Compiler Error Message: The compiler failed with error code 128. 的问题解决
- http://blog.csdn.net/name_Uncle_Wang/article/details/46438641?ref=myread
- 出现set_locale() failed (5): Input/output error的解决方法
- scanf问题
- Climbing Stairs爬楼梯——动态规划
- 9 Ways to Instantly Strengthen Your Brain