Leetcode 存在重复元素 (219,220)
219. 存在重复元素 II
给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k。
// 实现原理:这里面要求的一点是,其距离问题,也就是最大为K,name也就是说只要在距离的K的范围内,找到重复元素
// 即返回true,同样的范围已经大于K值的时候,这时候就要更新序列的起始位置。使用双指针策略进行。
public boolean containsNearbyDuplicate(int[] nums, int k) {
int low=0;
int high=0;
Set<Integer> result=new HashSet<>();
for(int i=0;i<nums.length;i++){
if(!result.contains(nums[i])){
high++;
result.add(nums[i]);
}
else if(high-low<=k){
return true;
}
if(high-low>k){
low++;
result.remove(nums[low]);
}
}
return false;
}
220. 存在重复元素 III
给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。
/*
* 借助treeset的subset函数,看看是否有在对应区间内的数据
*同时维护一个长度为K的窗口,超过这个窗口,就将其最前面的元素将其拿掉。
* */
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (nums==null || nums.length<2 || k<1|| t<0){
return false;
}
TreeSet<Long> result=new TreeSet<>();
for(int i=0;i<nums.length;i++){
SortedSet<Long> set=result.subSet((long)nums[i]-t,true, (long)nums[i]+t,true);
if(!set.isEmpty()){
return true;
}
else if(i>=k){
result.remove((long)nums[i-k]);
}
result.add((long)nums[i]);
}
return false;
}
暴力求解:
public boolean containsNearbyAlmostDuplicate2(int[] nums, int k, int t) {
int j=0;
for(int i=0;i<nums.length;i++){
j=i+1;
while(j<nums.length && j-i<=k){
int data1=nums[j];
int data2=nums[i];
if(Math.abs(data1-data2)<=t){
return true;
}
j++;
}
}
return false;
}
217. 存在重复元素
给定一个整数数组,判断是否存在重复元素。
如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。
策略:
对数组进行预排序
public boolean containsDuplicate(int[] nums) {
// 对数组进行排序o(nlogn)
Arrays.sort(nums);
boolean same=false;
if(nums==null ){
return true;
}
int i=0;
while(i<nums.length){
if(i==nums.length-1){
break;
}
if(nums[i]==nums[i+1]){
return true;
}
i++;
}
return false;
}
Leetcode 存在重复元素 (219,220)的更多相关文章
- leetcode 存在重复元素
给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3,1] 输出: true ...
- [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 ...
- Java实现 LeetCode 220 存在重复元素 III(三)
220. 存在重复元素 III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最 ...
- Java实现 LeetCode 219 存在重复元素 II(二)
219. 存在重复元素 II 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. 示 ...
- Leetcode 220.存在重复元素III
存在重复元素III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. ...
- [LeetCode] 217. Contains Duplicate 包含重复元素
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- python(leetcode)-重复元素算法题
leetcode初级算法 问题描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 该问题表述非常简单 ...
- LeetCode:存在重复元素【217】
LeetCode:存在重复元素[217] 题目描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 ...
随机推荐
- 19.3.20 解决pycharm快捷键无法使用问题和熟悉git与码云操作流程
problem:由于Vim插件导致快捷键无法使用: answer:settings→Plugins→搜索到ideaVim→取消选中→apply→重启pycharm: git:创建仓库→生成公钥(ssh ...
- scheduling while atomic和bad: scheduling from the idle thread(转)
https://blog.csdn.net/shanzhizi/article/details/22949121 https://blog.csdn.net/wwwtovvv/article/deta ...
- js考察this,作用域链和闭包
在严格版中的默认的this不再是window,而是undefined. 先看两个例子 example one var num = 20; var obj = { num: 30, fn: (funct ...
- python函数把可变数据类型当默认参数值的问题(转)
add by zhj: 文章写的很好,其实只要默认参数在函数中只读不写,那默认值就不会被修改,可变类型做默认参数就不会有问题 方法二中,当result is None时,修改result的指向,不再指 ...
- Spring Boot(二):Spring-Data-JPA操作数据库( Hibernate)增删改查
一.Maven使用3.3.9版本或以上,选择Binary 版本 二.添加spring-data-jpa和数据库依赖,以oracle为例 三.添加连接数据库配置 四.新建model自动生成数据库表(不用 ...
- es6下 vue实例属性template不能使用
esm模式下 不能使用template,需要引入非esm的vue.js,查看vue源码的包的dist目录下 文件标有esm是支持ems,没有标记,就是不支持(这个知识,怎么说了,应该属于webpack ...
- Spark学习笔记11面向对象编程
面向对象编程 11.1 object类 11.1.1定义一个简单的类 11.1.2 field的getter与setter 定义类包含,定义类的field及方法.其格式如下 class Cla ...
- UGUI背包系统
在Unity3d中,UGUI提供了Scroll Rect.Grid Layout Group.Mask这三个组件,下面就给大家介绍下如何用这个三个组件来实现滚动视图. 首先放置好背包的背景图 在矩形线 ...
- ORACLE 数据库管理
[故障处理]ORA-12162: TNS:net service name is incorrectly specified 本文将给大家阐述一个因未设置系统环境变量ORACLE_SID导致ORA-1 ...
- Python socket的客户端
做一个socket客户端1.声明一个实例2.绑定端口号和地址3.循环发送和接收响应其中要注意粘包的产生,为了防止粘包的产生,应该在服务器端先测出要发送信息的大小,然后发送响应至客户端,等到服务器上一条 ...