###### fantasia的博客

Somewhere on this planet

### 217. Contains Duplicate

Total Accepted: 92421 Total Submissions: 221846 Difficulty: Easy

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.

hashmap或者hashset。有重复就return。

public class Solution {
public boolean containsDuplicate(int[] nums) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<nums.length; i++){
if(!map.containsKey(nums[i])) map.put(nums[i],1);
else return true;
}
return false;
}
}

public class Solution {
public boolean containsDuplicate(int[] nums) {
int len = nums.length;
if (len==0) return false;

HashSet <Integer> set = new HashSet <Integer>();
for (int i=0; i< len; i++){
if (set.contains(nums[i])) return true;
}
return false;
}
}

### 219. Contains Duplicate II

Total Accepted: 60133 Total Submissions: 198719 Difficulty: Easy

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j]

and the difference between i and j is at most k.

public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i<nums.length; i++){
if(!map.containsKey(nums[i])) map.put(nums[i],i);
else if(i-map.get(nums[i])<=k) return true;
else map.put(nums[i], i);
}
return false;
}
}

### 220. Contains Duplicate III

Total Accepted: 28920 Total Submissions: 154344 Difficulty: Medium

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] and

nums[j] is at most t and the difference between i and j is at most k.

1. t<0应该直接返回false。

2. -t和+t时需要处理overflow。

public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (t<0) return false;
TreeSet<Integer> set = new TreeSet<Integer>();
int start=0;
for(int i=0; i<nums.length; i++){
if(i-start>k) set.remove(nums[start++]); // make sure we have k elements in the set at most

// version 1: use long, remember to use (long) to convert type
// long l = (long) nums[i]-t; long h = (long) nums[i]+t;
// int low = l < Integer.MIN_VALUE? Integer.MIN_VALUE : nums[i]-t;
// int high = h > Integer.MAX_VALUE? Integer.MAX_VALUE : nums[i]+t;

// verson 2: smarter way: a - b < c, a - b too small, then use b + c so that nothing overflow
int low = nums[i] < Integer.MIN_VALUE + t ? Integer.MIN_VALUE : nums[i]-t;
int high = nums[i] > Integer.MAX_VALUE - t ? Integer.MAX_VALUE : nums[i]+t;

// make sure the element is in range [-t, t], ceiling return the NEXT element larger than low
if(set.ceiling(low) !=null && set.ceiling(low)<=high) return true;
}
return false;
}
}

public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums.length < 2 || k < 1 || t < 0) return false;

ValuePosPair[] valPosArr = new ValuePosPair[nums.length];
for(int i = 0; i < nums.length; i++) valPosArr[i] = new ValuePosPair(nums[i],i);
Arrays.sort(valPosArr);

// [-3,3]
// 2
// 4
// id -  1 !=i make sure we do have a target, because we need the target to be in range of nums[i], nums[i]+t

for(int i = 0; i < valPosArr.length; i++){
int id = search(valPosArr, i, nums.length, valPosArr[i].val + t);
if(id != -1 && id - 1 != i && id - 1 >= 0 && id - 1 < nums.length && Math.abs(valPosArr[i].pos - valPosArr[id - 1].pos) <= k)
return true;
}
return false;
}

// first value > t, r is len
int search(ValuePosPair[] vs, int l, int r, long t){
if(l > r) return -1;
if(l == r) return vs[l].val > t ? l : -1;
while(l < r){
int m = l + (r - l) / 2;
if(vs[m].val <= t) l = m + 1;
else r = m;
}
return r;
}

class ValuePosPair implements Comparable<ValuePosPair>{

int val;
int pos;

ValuePosPair(int v, int p) { val = v; pos = p;}

public int compareTo(ValuePosPair x){
return this.val - x.val;
}
}
}  

#### Leetcode 220. Contains Duplicate III

2017-02-10 14:08:46

#### leetcode 220: Contains Duplicate III

2015-06-02 08:31:05

#### leetcode笔记：Contains Duplicate III

2016-02-26 15:43:16

#### Leetcode 220 Contains Duplicate III

2015-06-24 19:38:02

#### [leetcode] 220. Contains Duplicate III 解题报告

2016-01-30 11:09:05

#### 220 Contains Duplicate III

2015-06-18 17:19:53

#### LeetCode 220. Contains Duplicate III

2016-06-24 11:21:59

#### (leetcode)contains duplicateIII[java]

2015-07-03 23:36:33

#### Contains Duplicate III

2015-08-23 16:20:29

#### LeetCode Contains Duplicate III

2015-06-15 14:02:42

## 不良信息举报

Leetcode 217. Contains Duplicate & 219. Contains Duplicate II & 220. Contains Duplicate III