-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc-442.java
More file actions
33 lines (33 loc) · 843 Bytes
/
lc-442.java
File metadata and controls
33 lines (33 loc) · 843 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
/*
*用hashset太犯规了,考虑用别的办法吧
*
*/
class Solution {
public List<Integer> findDuplicates(int[] nums) {
Set<Integer> s = new HashSet();
List<Integer> res = new ArrayList();
for(int num : nums) {
if(s.contains(num)) {
res.add(num);
}else{
s.add(num);
}
}
return res;
}
}
//解法2,没有用set,数组值与下标的使用
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList();
for(int i = 0; i<nums.length; i++) {
int index = Math.abs(nums[i]) - 1;
if(nums[index]<0) {
res.add(index+1);
}else {
nums[index] *= -1;
}
}
return res;
}
}