-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
442.java
36 lines (35 loc) · 1.2 KB
/
442.java
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
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> ans = new ArrayList<>();
int[] mem = new int[nums.length+1];
for (int i = 0; i < nums.length; i++) {
if (mem[nums[i]] == 1) {
ans.add(nums[i]);
} else {
mem[nums[i]]++;
}
}
return ans;
}
}
__________________________________________________________________________________________________
sample 46304 kb submission
class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res=new ArrayList();
for(int i=0;i<nums.length;i++){
while(nums[i]!=nums[nums[i]-1]){
int t=nums[i];
nums[i]=nums[nums[i]-1];
nums[t-1]=t;
}
if(nums[i]==nums[nums[i]-1]&&nums[i]-1!=i&&!res.contains(nums[i])){
res.add(nums[i]);
}
}
return res;
}
}
__________________________________________________________________________________________________