<LeetCode OJ> 219. Contains Duplicate II
2016-01-09 22:49
519 查看
219. Contains Duplicate II
My SubmissionsQuestion
Total Accepted: 40529 Total
Submissions: 142658 Difficulty: Easy
有一个数组和一个整数,如果数组中两个相同的元素(分别在数组中i和j的位置),如果判断是否|i-j|<=k,若是则返回真
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.
Subscribe to see which companies asked this question
Hide Tags
Array Hash
Table
Hide Similar Problems
(E) Contains Duplicate (M)
Contains Duplicate III
//思路首先:哈希map(不要用红黑树map) //遍历数组,首先看当前元素是否在map中,如不在则压入,若在看是否其对应下标和当前下标相距为k //如果不则将原元素修改为现在的下标 class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { if(nums.empty()) return false; unordered_map<int,int> umapping; umapping[nums[0]]=0; for(int i=1;i<nums.size();i++) { //if(umapping.count(nums[i]) == 0)//没有出现(统计函数) if(umapping.find(nums[i]) == umapping.end())//直接查找 umapping[nums[i]]=i; else if((i-umapping[nums[i]])<=k)//相距小于k return true; else umapping[nums[i]]=i; } return false; } };
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50490187
原作者博客:http://blog.csdn.net/ebowtang
相关文章推荐
- Codeforces 615B Longtail Hedgehog (Round #338 (Div. 2) B题)
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog DP
- int main(int argc,char* argv[])详解
- clang: error: linker command failed with exit code 1 (use -v to see invocation)错误问题
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog(LIS)
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars
- AI学习笔记和制作棋子的教程
- [leetcode oj 217]Contains Duplicate
- Codeforces Round #338 (Div. 2) 615B Longtail Hedgehog(dp)
- Codeforces 615C:Longtail Hedgehog
- main函数的autoreleasepool有何用处
- LightOJ1021 Painful Bases(状压DP)
- 异步AIO的研究
- scu - 3254 - Rain and Fgj(最小点权割)
- 蓝牙写入出错,error isError Domain=CBATTErrorDomain Code=114 "Unknown ATT error."
- rails书籍展示添加游客评论
- Wait--使用sys.dm_io_virtual_file_stats来查看IO延迟
- 从notify-wait模式说去
- Contains Duplicate II leetcode
- 【转载】Kafka High Availability