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)的更多相关文章

  1. leetcode 存在重复元素

    给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3,1] 输出: true ...

  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 ...

  3. [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 ...

  4. Java实现 LeetCode 220 存在重复元素 III(三)

    220. 存在重复元素 III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最 ...

  5. Java实现 LeetCode 219 存在重复元素 II(二)

    219. 存在重复元素 II 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 i 和 j 的差的绝对值最大为 k. 示 ...

  6. Leetcode 220.存在重复元素III

    存在重复元素III 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ. ...

  7. [LeetCode] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  8. python(leetcode)-重复元素算法题

    leetcode初级算法 问题描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 该问题表述非常简单 ...

  9. LeetCode:存在重复元素【217】

    LeetCode:存在重复元素[217] 题目描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 ...

随机推荐

  1. Oracle 用户权限 Grant

    用户的权限来自系统权限和对象权限 一.系统权限 3个索引权限 Grant CREATE ANY INDEX to User_Name://创建索引 Grant ALTER ANY INDEX to U ...

  2. Spring-day02

    Annotation复习:1,Annotation:作为类型的元数据; 1,给类型加标记; 2,annotation可以添加各种类型的属性;2,Annotation的上的标记: 1),target:标 ...

  3. Linux命令 at cron

    at: 可以处理仅执行一次就结束排程的指令.需要atd服务 crontab: 所设定的指令将会循环地一直进行下去.需要crontab服务 at: Ubuntu16.04 默认没有安装atd服务.安装命 ...

  4. flink with rabbitmq,sink source mysql redis es

    flink-dockerhttps://github.com/melentye/flink-docker https://shekharsingh.com/blog/2016/11/12/apache ...

  5. robotframework RF使用中需要安装的工具和库

    确保 Python 3.6.2 安装成功 安装 如下 RF使用中需要的工具和库 1. RF 在两个Python中安装 robotframework执行命令 pip install robotframe ...

  6. 2018-2019-2 网络对抗技术 20165317 Exp2 后门原理与实践

    2018-2019-2 网络对抗技术 20165317 Exp2 后门原理与实践 基础问题回答 例举你能想到的一个后门进入到你系统中的可能方式? 下载免费应用的时候会有绑定木马. 浏览某些网页时会有内 ...

  7. js的一些注意点

    18-12-24 oninput事件: 在用户输入时触发,它是在元素值发生变化时立即触发: 该事件在 <input> 或 <textarea> 元素的值发生改变时触发. 缺陷: ...

  8. springcloud第三步:发布服务消费者

    服务消费者 创建项目sercice-order Maven依赖 <parent> <groupId>org.springframework.boot</groupId&g ...

  9. 《图解HTTP》读书笔记(二:各种协议与HTTP协议之间的关系)

    涉及到DNS协议.TCP协议.IP协议,话不多说,上图:

  10. python基础语法-->多项分支-->巢状分支

    # ### 多项分支 """ if 条件表达式: codel1... codel1... else 条件表达式 coedl2.. coedl2.. else 条件表达式 ...