Contains Duplicate I & III
Contains Duplicate I
Given an array of integers, find if the array contains any duplicates.
Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Example 1:
Input: [1,2,3,1]
Output: true
Example 2:
Input: [1,2,3,4]
Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2]
Output: true
public boolean containsDuplicate(int[] nums) {
Set<Integer> distinct = new HashSet<>();
for(int num : nums) {
if(distinct.contains(num)) {
return true;
}
distinct.add(num);
}
return false;
}
Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] andnums[j] is at most t and the difference between i and j is at most k.
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums==null||nums.length<||k<||t<)
return false; TreeSet<Long> set = new TreeSet<Long>();
for(int i=; i<nums.length; i++){
long curr = (long) nums[i]; long leftBoundary = (long) curr-t;
long rightBoundary = (long) curr+t+; //right boundary is exclusive, so +1
SortedSet<Long> sub = set.subSet(leftBoundary, rightBoundary);
if(sub.size()>)
return true; set.add(curr); if(i>=k){ // or if(set.size()>=k+1)
set.remove((long)nums[i-k]);
}
} return false;
}
}
Contains Duplicate I & III的更多相关文章
- [LeetCode] Contains Duplicate(II,III)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- Contains Duplicate,Contains Duplicate II,Contains Duplicate III
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- Contains Duplicate III -leetcode
Contains Duplicate III Given an array of integers, find out whether there are two distinct indices i ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 220. Contains Duplicate III
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- [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 ...
- [Leetcode] Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
随机推荐
- JavaSE_List&Array_Java1.7
这里简单写了下List和Array的相互转换 package cn.rocker.collection.list; import org.junit.Test; import java.util.Ar ...
- OGNL中的#、%和$符号的用法
转自:https://blog.csdn.net/qq_24963197/article/details/51773224 一.OGNL中的#.%和$符号 1.#符号的三种用法 1)访问非根对象属性, ...
- 【.NET】asp.net Redirect 图片路径
#需求: 前端通过<img>的src向服务端请求图片信息,如果不存在想要的图片,那么就返回一张默认路径下的图片: #实现: <img class="related_reso ...
- python django基础五 ORM多表操作
首先在创建表的时候看下分析一下 1.作者表和作者详细地址表 一对一关系 理论上谁都能当主表 把Author设置成主表 au=models.OneToOneField(to='AuthorDetail ...
- thymeleaf 基础
(一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下 ...
- java中获取request与response对象的方法
Java 获取Request,Response对象方法 第一种.参数 @RequestMapping("/test") @ResponseBody public void sa ...
- 网络编程之Socket & ServerSocket
网络编程之Socket & ServerSocket Socket:网络套接字,网络插座,建立网络通信连接至少要一对端口号(socket).socket本质是编程接口(API),对TCP/IP ...
- redis集群之主从架构
https://redis.io/topics/replication1. redis主从架构概述(1)一个master可以配置多个slave(2)slave与master之间使用异步复制进行数据同步 ...
- tomcat cluster session同步时保存map数据遇到的问题
Tomcat Cluster官网:https://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html(tomcat7.0) 场景: tomcat1 ...
- C#生成Guid,SqlServer生成Guid
https://www.cnblogs.com/che109/p/6808143.html工作中需要用到全球唯一标识符,在.net当中 微软已经为我们添加了此方法,我们只需要直接调用即可.代码如下: ...