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. 【java】i++与++i、i--运算

    package test; //i++与--i运算 public class test { public static void main(String[] args) { int b=0; int ...

  2. layer过去的时间不能选择,只能选择未来的时间 LayUI中的时间日期控件,设置时间范围,

    默认Layui中的时间控件显示如下: 我当时系统时间是2018-06-07, 我需要做的是2018-06-07之后过去的时间不能选择 <p><span>时间范围:</sp ...

  3. String对象方法属性总结

    常用属性: constructor;length;prototype;(不在解释): 常用方法: charAt(index);返回指定位置的字符. concat(stringX);连接字符串. ind ...

  4. oc与c语言的相互调用

    一:OC调用C语言 C语言的.h文件 // // TestPrint.h // TestDemo // // Created by Techsun on 14-8-12. // Copyright ( ...

  5. 107个JS常用方法(持续更新中)

    1.输出语句:document.write(""); 2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head,body)4.一个 ...

  6. linux --- Ansible篇

    ansible背景 1.什么是ansible? ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优 ...

  7. 解决 AutoMapper ProjectTo 不起作用的问题

    这两天在一个 ASP.NET Core 项目中遭遇了 AutoMapper ProjectTo 不起作用的奇怪问题,虽然在 ProjectTo 中指定了 DTO ,但 EF Core 生成的 SQL ...

  8. django的分页与添加图片

    分页: 在主页面的views里写接口 导包: from django.core.paginator import Paginator 接口: id=request.GET.get("page ...

  9. [Day24]IO(转换流、缓冲流)

    1. 转换流 1.1 OutputStreamWriter类-字符流通向字节流的桥梁,可使用指定的字符编码表,将要写入流中的字符编码成字节. 1.2 InputStreamReader类-字节流通向字 ...

  10. UML用例关系一览