每天一算:Contains Duplicate II

描述

给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k

Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true.

Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true

Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false

解析

考虑用滑动窗口与查找表来解决。

  • 设置查找表record,用来保存每次遍历时插入的元素,record的最大长度为k

  • 遍历数组nums,每次遍历的时候在record查找是否存在相同的元素,如果存在则返回true,遍历结束

  • 如果此次遍历在record未查找到,则将该元素插入到record中,而后查看record的长度是否为k + 1

  • 如果此时record的长度是否为k + 1,则删减record的元素,该元素的值为nums[i - k]

  • 如果遍历完整个数组nums未查找到则返回false

代码

public static boolean containsDuplicate2(int[] n, int k) {
if (n == null || n.length < k) {
return false;
}
List<Integer> list = new ArrayList<>(k);
for (int i = 0; i < n.length; i++) {
if (!list.contains(n[i])) {
list.add(n[i]); if (list.size() > k) {
list.remove(0);
}
} else {
return true;
}
}
return false;
}

[LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)的更多相关文章

  1. [LeetCode] 219. Contains Duplicate II 包含重复元素 II

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  2. [LeetCode] 220. Contains Duplicate III 包含重复元素 III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  3. LeetCode 219. Contains Duplicate II (包含重复项之二)

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  4. C#解leetcode 219. Contains Duplicate II

    该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...

  5. [LeetCode] 219. Contains Duplicate II 解题思路

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  6. 219 Contains Duplicate II 存在重复 II

    给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使 nums [i] = nums [j],并且 i 和 j 的绝对差值最大为 k. 详见:https://leetcod ...

  7. LeetCode 219 Contains Duplicate II

    Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...

  8. Java for LeetCode 219 Contains Duplicate II

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  9. Leetcode 219 Contains Duplicate II STL

    找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...

随机推荐

  1. ES6内建对象的继承

    在ES6之前, JS是无法通过继承的方式创建属于自己的特殊数组的, 也就是说可以使用原型链来继承数组的一些方法, 但是某些返回一个数组的方法返回的值还是一个Array的实例, 例如slice, len ...

  2. 采购订单审批与撤销审批BAPI

    *"---------------------------------------------------------------------- *"*"本地接口: *& ...

  3. mysql定时任务event——清理过期数据 (转)

    1.查询是否开启事件调度 SHOW VARIABLES LIKE 'event_scheduler'; 2.开启事件调度 临时: SET GLOBAL event_scheduler = 1; 永久: ...

  4. centos6.8yum 安装mysql

    1:查看是否已有mysql版本 rpm -qa | grep mysql 删除mysql 账号和用户组 删除/etc/my.cnf 2:有的话就删除 rpm -e mysql-...... 或者 co ...

  5. 蒙特卡罗方法计算pi

    import scala.math.random object LocalPi { def main(args: Array[String]) { var count = 0 for (i <- ...

  6. c# 结构体struct注意事项

    c# 结构struct注意事项 1,不能定义无参构造函数,因为隐式的默认无参构造函数不能被重写 2,当为某个结构编写带有参数的构造函数时,必须显式初始化所有成员,否则编译不过. 3,不允许在结构的实例 ...

  7. iOS-AVFoundation生成缩略图

    使用MPMoviePlayerController来生成缩略图足够简单,但是如果仅仅是是为了生成缩略图而不进行视频播放的话,此刻使用 MPMoviePlayerController就有点大材小用了.其 ...

  8. iOS-NSURLConnection异步发送 HTTP请求

    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { NSString *new = [searchBar.text stri ...

  9. (1)、JEasyUI 之 Datagrid的Combobox 显示 textField 值的问题

    (1).JEasyUI 之datagrid的Combobox显示textField值的问题 官方的datagrid Demo Row Editing in DataGrid 中field 是否如下定义 ...

  10. Egret入门学习日记 --- 第十二篇(书中 5.1节 内容)

    第十二篇(书中 5.1节 内容) 昨天把 第4章完成了. 今天来看第5章. 接下来是 5.1节 的内容. 总结一下 5.1节 的重点: 1.如何制作一个公用按钮皮肤. 跟着做: 重点1:如何制作一个公 ...