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. 【zc】 PHP中json_encode(编码) 与 json_decode(解码) 【aa】

    一.json_encode() 对变量进行JSON编码, 语法: json_encode ( $value [, $options = 0 ] ) 注意:1.$value为要编码的值,且该函数只对UT ...

  2. aspose 生成word 简单的文档操作

    package aspose.com.word; import com.aspose.words.Document;import com.aspose.words.DocumentBuilder; p ...

  3. Android总结之json解析(FastJson Gson 对比)[申明:来源于网络]

    Android总结之json解析(FastJson Gson 对比)[申明:来源于网络] 地址:http://blog.csdn.net/u014031072/article/details/5392 ...

  4. What's the meaning of unqualified-id?

    catch( const std::runtime_error & e) { .... } When compile, met an error: error: expected unqual ...

  5. u-boot调试串口输出对应的系统函数

    接上Debug串口,启动机器,u-boot哗啦啦地打印一行行的字符.刚接触u-boot的时候,对机器后台做了什么,几乎一无所知. 如果要很有信心地定制出一个简单并且可靠的系统,或者快速完成一项新的任务 ...

  6. WangEditor+thinkphp5【真实可用+原创】

    今天公司要编辑文章,一开始准备用ueditor,但是到了linux环境下一直不行,所以最终放弃.改用另外一个编辑器WangEditor.更加轻量级. 遇到最大的问题是 一个是图片上传,一个是div中的 ...

  7. Mock Server 实现post方法的接口(三)

    Mock Server 实现post方法的接口(三) 1.mock server实现的接口,当request中未设置"method"时,会自动将所有method试一次,所以一定要指 ...

  8. find 详解

    find 详解 原文请访问http://itlab.idcquan.com/linux/SHELL/949102.html 当最初登录到系统中时, u m a s k命令确定了你创建文件的缺省模式.这 ...

  9. Map集合转成json数据

    maven项目需要导入一下依赖: <dependency> <groupId>net.sf.json-lib</groupId> <artifactId> ...

  10. jedis & common pool

    http://mvnrepository.com/artifact/redis.clients/jedis http://mvnrepository.com/artifact/org.apache.c ...