[LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
描述
给出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)的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- C#解leetcode 219. Contains Duplicate II
该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...
- [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 ...
- 219 Contains Duplicate II 存在重复 II
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使 nums [i] = nums [j],并且 i 和 j 的绝对差值最大为 k. 详见:https://leetcod ...
- LeetCode 219 Contains Duplicate II
Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...
- 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 ...
- Leetcode 219 Contains Duplicate II STL
找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...
随机推荐
- zabbix4.4 (server,proxy,agent)安装部署实战
一. 部署架构图 二. 安装环境及版本信息 操作系统:centos7 zabbix版本: 4.4 mysql: 5.7.28 三.zabbix server安装(192.168.182.132) rp ...
- LeetCode_167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sorted Easy Given an array of integers that is already sorted in as ...
- ALBPM Service Config
ALBPM Config About ALBPM Studio Msg , JSP and webResources together deploy services. Msg "D ...
- jqGrid全部选中
var jqGrid = $("#jqGrid"); // 拿到所有行id var jqGridIDs = jqGrid.getDataIDs(); // 拿到所有选中行id va ...
- 4、1 IK分词器
我们在浏览器地址栏输入 http://127.0.0.1:9200/_analyze?analyzer=chinese&pretty=true&text=我是程序员 默认的中文分词是将 ...
- BBC这10部国宝级纪录片,让孩子看遍世间最美的地方
https://weibo.com/ttarticle/p/show?id=2309404382383649486138#related
- java 微信开发的工具类WeChatUtils
import com.alibaba.fastjson.JSONObject;import com.bhudy.entity.BhudyPlugin;import com.bhudy.service. ...
- windows和linux环境下使用google的glog日志库
一.概述 glog是google推出的一款轻量级c++开源日志框架,源码在github上,目前最新release版本是v0.3.5. githut地址:https://github.com/googl ...
- C++Primer 5th Chap9 Sequential Container
vector 可变大小数组,支持快速随机访问(在除了尾部之外部分插入删除元素很慢) deque 双端队列,支持快速随机访问(在头尾插入删除元素很快) list 双向链表,仅支持双向顺序访问(在任何位置 ...
- python对影评进行评论分析,形成词云图
1 # -*- coding:utf-8 -*- 2 ''' 3 抓取豆瓣电影某部电影的评论 4抓取电影 5 网址链接:https://movie.douban.com/subject/ ...