forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainsDuplicate.java
More file actions
33 lines (29 loc) · 803 Bytes
/
ContainsDuplicate.java
File metadata and controls
33 lines (29 loc) · 803 Bytes
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
package leetcode;
import java.util.Arrays;
import java.util.HashSet;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : ContainsDuplicate
* Creator : Edward
* Date : Oct, 2017
* Description : 217. Contains Duplicate
*/
public class ContainsDuplicate {
// time : O(n) space : O(n)
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if (!set.add(nums[i])) return true;
}
return false;
}
// time : O(nlogn) space : O(1)
public boolean containsDuplicate2(int[] nums) {
Arrays.sort(nums);
for (int i = 1; i < nums.length; i++) {
if (nums[i] == nums[i - 1]) return true;
}
return false;
}
}