Contains Duplicate II
来源:互联网 发布:威纶通触摸屏编程手册 编辑:程序博客网 时间:2024/05/29 19:06
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 j is at most k.
思路:哈希表问题,代码如下:
class Solution {public: bool containsNearbyDuplicate(vector<int>& nums, int k) { map<int, int>hmap; int size = nums.size(); for(int i = 0; i < size; ++i){ if(hmap.count(nums[i])){ int index = hmap[nums[i]]; if(i - index <= k) return true; hmap[nums[i]] = i; } else hmap.insert(pair<int, int>(nums[i], i)); } return false; }};
0 0
- Contains Duplicate && Contains Duplicate II
- [LeetCode]Contains Duplicate II
- Contains Duplicate II
- [LeetCode] Contains Duplicate II
- leetcode Contains Duplicate II
- LeetCode Contains Duplicate II
- [leetcode] Contains Duplicate II
- LeetCode Contains Duplicate II
- LeetCode219:Contains Duplicate II
- #219 Contains Duplicate II
- 219. Contains Duplicate II
- 【leetcode】Contains Duplicate II
- leetcode--Contains Duplicate II
- 219Contains Duplicate II
- Contains Duplicate II
- Contains Duplicate II
- Contains Duplicate II
- Leetcode_219 Contains Duplicate II
- HDU 1166 敌兵布阵【线段树】
- catalina.out文件过大
- CMake 基本使用方法--写CMakeList.txt
- Spring MVC 全注解的方式实现单个和多个文件上传
- performSelector多参数
- Contains Duplicate II
- LA4015 Caves
- markdown字体字号背景色
- LeetCode---Remove Nth Node From End of List
- 杨辉三角python实现
- Apple Watch如何进行真机调试及问题汇总
- Linux Epoll介绍和程序实例
- 严蔚敏数据结构习题3.31
- 开启JBoss as 7的access_log功能