-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path287. Find the Duplicate Number
66 lines (52 loc) · 1.08 KB
/
287. Find the Duplicate Number
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
# Answer-1
Runtime 4 ms
Beats 87.86%
Memory 51.8 MB
Beats 99.95%
class Solution {
public int findDuplicate(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int k=Math.abs(nums[i]);
if(nums[k]<0){System.gc();return k;}
nums[k]*=-1;
}
return 0;
}
}
# Answer-2
Runtime 3 ms
Beats 92.21%
Memory 57.5 MB
Beats 23.54%
class Solution {
public int findDuplicate(int[] nums) {
int count[] = new int [nums.length];
for(int i = 0; i<nums.length; i++){
count[nums[i]]++;
if(count[nums[i]] > 1){
return nums[i];
}
}
return nums.length;
}
}
# Answer-3
Runtime 2 ms
Beats 98.59%
Memory 57.5 MB
Beats 21.82%
class Solution {
public int findDuplicate(int[] nums) {
int res=0,i=0,c=0;
int[] fre=new int[nums.length];
for(;i<nums.length;i++){
c=nums[i];
fre[c-1]++;
}
for(i=0;i<nums.length;i++){
if(fre[i]>1)
res=i+1;
}
return res;
}
}