-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path645. Set Mismatch
67 lines (44 loc) · 1.42 KB
/
645. Set Mismatch
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
/*Runtime: 2 ms, faster than 86.18% of Java online submissions for Set Mismatch.
Memory Usage: 40 MB, less than 96.41% of Java online submissions for Set Mismatch.*/
class Solution {
public int[] findErrorNums(int[] nums) {
int res[]=new int[2];
int count[]=new int[10001];
for(int i=0;i<nums.length;i++)
++count[nums[i]];
for(int i=0;i<count.length;i++)
if(count[i]>1)
res[0]=i;
int i=1;
while(count[i]!=0 ){
res[1]=i;
i++;
}
res[1]++;
return res;
}
}
/*Runtime: 11 ms, faster than 34.11% of Java online submissions for Set Mismatch.
Memory Usage: 40.4 MB, less than 80.99% of Java online submissions for Set Mismatch.*/
class Solution {
public int[] findErrorNums(int[] nums) {
int[] arr=new int[2];
HashSet<Integer> set= new HashSet<>();
for(int i:nums){
if(set.contains(i))
arr[0]=i;
set.add(i);
}
for(int i=0;i<set.size();++i)
{
if(!set.contains(i+1))
{
arr[1]=i+1;
break;
}
}
if(arr[1]==0)
arr[1]=nums.length;
return arr;
}
}