-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContains Duplicate.java
More file actions
84 lines (69 loc) · 2.11 KB
/
Contains Duplicate.java
File metadata and controls
84 lines (69 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
217. Contains Duplicate
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.
*/
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
boolean status=false;
int count=0;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (nums[i]==nums[j])
count++;
if(count>1) {
status=true;
break;
}
}
count=0;
}
return status;
}
//上面的暴力O(n^2)比较很显然是超时的,来个O(n)
public static boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
boolean status=false;
for (int i = 1; i < nums.length; i++) {
if(nums[i] == nums[i-1]) {
status = true;
break;
}
}
return status;
}
/*
219. Contains Duplicate II
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 absolute difference
between i and j is at most k.
*/
//第一遍:Time Limit Exceeded
public boolean containsNearbyDuplicate(int[] nums, int k) {
boolean status = false;
for (int i = 0; i < nums.length-1; i++) {
for (int j = i+1; j < nums.length; j++) {
if(j-i > k)
break;
if (nums[i] == nums[j] && j-i <=k) {
status = true;
break;
}
}
}
return status;
}
/*
既然第一种算法该优化的也优化了,还是不行,那就只能改变思路了(其实也没有改变思路,只是采用了set集合),
可以采用set集合,因为它有这个特性:
如果 set 中尚未存在指定的元素,则添加此元素;如果此 set 已经包含该元素,则该调用不改变此 set 并返回 false。
*/
public static boolean containsNearbyDuplicate(int[] nums, int k) {
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < nums.length; i++) {
if(i>k) set.remove(nums[i-k-1]);
if(!set.add(nums[i])) return true;
}
return false;
}