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. 示例 ...
随机推荐
- Spring-注入
一.Spring的基本介绍:Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建.简单来说,Spring是一个分层的JavaSE/ ...
- Linux系统安装tomcat
1.首先下载tomcat:http://tomcat.apache.org/download-60.cgi 2.解压缩tar.gz文件: tar -xzvf xxxxxxx/apache-tomcat ...
- 洛谷P1996 约瑟夫问题【链表】
题目:https://www.luogu.org/problemnew/show/P1996 题意: 约瑟夫环.每次取出第m个,第2m个...... 思路: 链表维护.[感觉很少有用到链表.]非常经典 ...
- instrument之Xcode Analyze 代码静态检查及问题解决
Static Code Analysis(静态代码分析)用来发现源代码潜在的错误与缺陷,源代码编译后只有在运行时有可能会产生细微的错误,他们可能难以识别和修复,所以这些潜在的威胁在开发过程中一定要尽可 ...
- Redis单机多节点集群实验
第一步:安装Redis 前面已经安装过了 不解释, Reids安装包里有个集群工具,要复制到/usr/local/bin里去 cp redis-3.2.9/src/redis-trib.rb /usr ...
- springcloud第四步:ribbon搭建服务负载均衡
使用ribbon实现负载均衡 启动两个会员服务工程,端口号分别为8762.8763,订单服务 使用负载均衡策略轮训到会员服务接口. 什么是ribbon ribbon是一个负载均衡客户端 类似nginx ...
- MySQL SQL Explain输出学习
MySQL的explain命令语句提供了如何执行SQL语句的信息,解析SQL语句的执行计划并展示,explain支持select.delete.insert.replace和update等语句,也支持 ...
- curl 支持 http2
让 curl 支持 HTTP2 我们需要安装 nghttp2(http2 的 C 语言库) 源码安装 安装 nghttp2 git clone https://github.com/tatsuhiro ...
- 面试 Java 高级后端开发,要准备哪些知识点?
其实公司肯花时间让你去面试,前提条件一定是通过你的简历,一定发现了你和公司的匹配点,也就是说,一定是有录用意向的. 在技术面试的时间段里(最长1个小时),你如果能展现你的优势那是最好的,但如果你做不到 ...
- Windows Java安装
jdk安装与配置jdk for windows1.下载官网地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html2. ...