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. Linux系统中/etc/rc.local和/etc/rc.d/rc.local的区别

    /etc/rc.d/rc.local 用于添加开机启动命令 /etc/rc.local是/etc/rc.d/rc.local的软连接

  2. springcloud配置需要主要的地方

    Eureka服务端 注册中心 <!-- Eureka服务端 --> <dependency> <groupId>org.springframework.cloud& ...

  3. python 面向对象(经典作业讲解)

    #############################总结######################## # lst =['python%s期' %i for i in range(510)]# ...

  4. css 动画【转】

    css 动画 http://www.w3school.com.cn/css3/css3_animation.asp

  5. LeetCode(192. Word Frequency)

    192. Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...

  6. Arrays.asList() 的使用注意

    Sometimes it is needed to convert a Java array to List or Collection because the latter is a more po ...

  7. CentOS7 安装 Tomcat8

    安装 Java8准备更新软件yum update 如果提示没有 wget 命令,那么必须先安装 wget 如下:yum install wget 安装Tomcat8这里采用离线解压tar.gz的方式安 ...

  8. windows server 2008r2x64 安全配置

    管理工具--本地安全策略 1.帐户策略--密码策略 密码长度最小值:8 强制密码历史:1 2.本地安全策略--安全选项 交互式登录:不显示最后的用户名:已启用

  9. Python实现代理模式

    from abc import ABCMeta, abstractmethod NOT_IMPLEMENTED = "You should implement this." cla ...

  10. Basic berkeley socket functions

    gethostbyname() DNS を通して.Domain の Information を GET する.例えば IP Address なんだ. げん型: #include <netdb.h ...