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

  1. [LeetCode] Contains Duplicate(II,III)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  2. Contains Duplicate III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  3. Contains Duplicate,Contains Duplicate II,Contains Duplicate III

    217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...

  4. Contains Duplicate III -leetcode

    Contains Duplicate III Given an array of integers, find out whether there are two distinct indices i ...

  5. [LeetCode] Contains Duplicate III 包含重复值之三

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  6. 220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

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

  9. [Leetcode] Contains Duplicate III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

随机推荐

  1. CodeForces12D 树状数组降维

    http://codeforces.com/problemset/problem/12/D 题意 给N (N<=500000)个点,每个点有x,y,z ( 0<= x,y,z <=1 ...

  2. Java 语言特性

    介绍以下几种语言特性: Java5的特性 1.静态引用 2.可变参数 3.自动装箱和拆箱  包装类的缓存设计 4.枚举 一.静态引用(语法糖,不推荐使用,了解一下即可) 先看看普通的引用,就是impo ...

  3. CSS-Naming-Conventions--BEM

    BEM BEM : Block Element Modifier There are only two hard problems in Computer Sciences:cache invalid ...

  4. HDU - 1540 Tunnel Warfare(线段树区间合并)

    https://cn.vjudge.net/problem/HDU-1540 题意 D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 分析 线段树的区间内,我 ...

  5. HDU 1115(求质量均匀分布的多边形重心 物理)

    题意是给一个 n 边形,给出沿逆时针方向分布的各顶点的坐标,求出 n 边形的重心. 求多边形重心的情况大致上有三种: 一.多边形的质量都分布在各顶点上,像是用轻杆连接成的多边形框,各顶点的坐标为Xi, ...

  6. ArcMap登录和ArcGIS online都是灰色,无法连接网络的解决方法

    此问题可能由多种原因造成,表现为系统托盘中的 ArcGIS Online 地球图标断开连接(显示红色×). 解决方法: 方法一: 在系统托盘中的 ArcGIS Online 地球图标右键单击此图标 & ...

  7. koa2学习笔记

    Koa -- 基于 Node.js 平台的下一代 web 开发框架 首先创建一个项目文件夹进行初始化 npm init -y 该命令执行后会生成一个package.json项目描述文件 安装koa n ...

  8. 【移动端】300ms延迟以及点透事件原因以及解决方案

    产生原因 移动端会有双击缩放的这个操作,因此浏览器在click之后要等待300ms,看用户有没有下一次点击,也就是这次操作是不是双击 说完移动端点击300ms延迟的问题,还不得不提一下移动端点击穿透的 ...

  9. 【转载】C# List用法 List介绍

    https://www.cnblogs.com/dyhao/p/9501479.html

  10. Java入门系列 Java 中的四种引用

    Why java内存管理分为内存分配和内存回收,都不需要程序员负责,垃圾回收的机制主要是看对象是否有引用指向该对象. java对象的引用包括强引用,软引用,弱引用,虚引用 Java中提供这四种引用类型 ...