LeetCode-Contains Duplicate II
2015-10-10 19:09
405 查看
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.
这道题牵涉到map容器的使用。主要的思想是,用map的查找优势,避免二重循环,即降低运算复杂度。
本题的答案来自“阳光的味道-辉”博主,链接:http://blog.csdn.net/sxhlovehmm/article/details/46552995
the difference between i and j is at most k.
这道题牵涉到map容器的使用。主要的思想是,用map的查找优势,避免二重循环,即降低运算复杂度。
本题的答案来自“阳光的味道-辉”博主,链接:http://blog.csdn.net/sxhlovehmm/article/details/46552995
class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { map<int,vector<int> > arrayMap; for(int i=0;i<nums.size();i++){ if(arrayMap.count(nums[i])==0){ vector<int> arrvec; arrvec.push_back(i); arrayMap.insert(pair<int,vector<int> >(nums[i],arrvec)); } else{ int length=arrayMap[nums[i]].size(); if(i-arrayMap[nums[i]][length-1]<=k)//符合条件则返回 return true; else arrayMap[nums[i]].push_back(i); } } return false; } };
相关文章推荐
- LightOJ 1138 Trailing Zeroes (III)
- WinMain与WndProc以及窗口诞生过程总结
- CClientDC和CPaintDC、CWindowsDC之间的联系和区别
- *SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
- 17.1.1.4 Obtaining the Replication Master Binary Log Coordinate
- HDU 4810 Wall Painting
- HDU 5228 ZCC loves straight flush
- 生产环境 分区做raid
- failed command: READ DMA
- crossdomain.xml怎么用
- Error Domain=NSCocoaErrorDomain Code=3840解决方法
- 【未完】mail邮件报警系统搭建
- property 描述 之copy assign retain strong weak
- MainData仿Backbone Model式 数据模型记录器
- ALAssetsLibrary的使用问题:获取到ALAsset的thumbnail或者其他属性保存数组,在别地地方为nil的问题处理
- UVA Live Archive 4394 String painter(区间dp)
- 网站的高可用架构 Availability
- 论assign copy retain readonly readwrite nonatomic weak strong各自的特点
- neutron(1)veth pair
- LightOJ 1236 Pairs Forming LCM