Contains Duplicate II 是否包含重复
来源:互联网 发布:网络安全教育开题报告 编辑:程序博客网 时间:2024/06/11 17:54
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.
跟前一题的差别是,它限制了重复的元素的index差在k范围内。
一个思路就是,在判断重复的set里面只放index 差在k范围内的元素。
运行时间:
代码:
public class ContainsDuplicateII { public boolean containsNearbyDuplicate(int[] nums, int k) { Set<Integer> set = new HashSet<>(); for (int i = 0; i < nums.length; i++) { // remove the element whose index are out of k if (i > k) { set.remove(nums[i - k - 1]); } if (!set.add(nums[i])) { return true; } } return false; }}
1 0
- Contains Duplicate II 是否包含重复
- [Java]Contains Duplicate II 包含重复数字
- Contains Duplicate 包含重复
- 219. Contains Duplicate II 包含重复元素2
- Contains Duplicate 数组中是否包含重复元素
- Contains Duplicate III 是否包含重复元素系列3
- LeetCode217 Contains Duplicate(判断数组是否包含重复值)
- Leetcode: 217. Contains Duplicate(数组是否包含重复数字)
- Contains Duplicate II-副本包含
- 【LeetCode-面试算法经典-Java实现】【219-Contains Duplicate II(包含重复元素II)】
- [Java]Contains Duplicate 包含重复数字
- 217. Contains Duplicate 包含重复元素
- LeetCode OJ 之 Contains Duplicate II (包含重复判断- 二)
- LeetCode 219 Contains Duplicate II(包含重复数字2)(*)
- Contains Duplicate II 重复数HASH
- leetcode217---Contains Duplicate(是否有重复数)
- (LeetCode)Contains Duplicate -- 查看数组是否重复
- Contains Duplicate II 找出数组中是否有重复元素,长度小于k
- 123
- OpenCV的所有模块介绍
- 数据库复习总结
- linux 服务器间copy 文件
- 办公软件Cnskype for business自动化系统的作用
- Contains Duplicate II 是否包含重复
- 关于mongoDB 失效数据 ttl ensureIndex
- HTTP原理之HTTP报文(1) 方法
- 阿里云服务器linux系统远程连接
- html——表单(1)
- Python list 基础知识篇
- New String(s) 和 s.toString() 的区别
- C++STL各种实现版本介绍
- XML字符串转换成XML Document对象,并生成在指定路径