-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path268Missing_Number.java
More file actions
54 lines (43 loc) · 1.13 KB
/
268Missing_Number.java
File metadata and controls
54 lines (43 loc) · 1.13 KB
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
// class Solution {
// public int missingNumber(int[] nums) {
// Arrays.sort(nums);
// for(int i = 0; i<nums.length; i++){
// if(nums[i] != i){
// return i;
// }
// }
// return nums.length;
// }
// }
class Solution {
public int missingNumber(int[] nums) {
int[] a = new int[nums.length+1];
for(int i = 0; i<nums.length; i++){
a[nums[i]] = -1;
}
for(int i = 0; i<nums.length; i++){
if(a[i] != -1){
return i;
}
}
return nums.length;
}
}
//hashset
// Set<Integer> set = new HashSet<>();
// for(int i = 0; i < nums.length; i++) set.add(nums[i]);
// for(int i = 0; i <= nums.length; i++)
// if(!set.contains(i)) return i;
// return -1;
//bit operation x⊕x=0 x⊕0=x, different 1, same 0.
// int res = nums.length;
// for(int i = 0; i < nums.length; i++){
// res ^= nums[i] ^ i;
// }
// return res;
//math sum
// int sum = 0;
// for(int i = 0; i < nums.length; i++){
// sum += nums[i];
// }
// return nums.length * (nums.length + 1) / 2 - sum;