Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathMissing Number.java
More file actions
Latest commit
executable file
·131 lines (107 loc) · 3.15 KB
/
Copy pathMissing Number.java
File metadata and controls
executable file
·131 lines (107 loc) · 3.15 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
E
1526012135
tags: Array, Math, BitManipulation
给一串unique数字, 数字取自 [0 ~ n], 无序, 找第一个skipped的数字.
#### Swap
- 跟FirstMissingPositive非常像, 只有一行代码的区别.
- swap所有的数字, 到自己的correctposition
- 最后一个forloop找到错位的index, 也就是缺的数字.
#### BitManipulation
- XORwillonlyretainbitsthataredifferent1 ^ 0 = 1, but0^0, 1^1 == 0
- Usethatfeature, 把所有value都和indexXOR了
- 剩下的多余的数字, 其实是那个index无法被XOR消掉, 也就是那个缺的numbervalue.
- 注意: 题目告诉数字是 [0 ~ n], 然而缺一个数字, 那么在[0 ~ n - 1] 里面, 最大的数字(不管缺没缺), 一定是n = nums.length.
#### HastSet
- 全存, 找missing
- O(n) space, 不合题意
#### sorting
- sort, 找1stmissing
- O(nlogn) 太慢, 不合题意
```
/*
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n,
find the one that is missing from the array.
Example 1:
Input: [3,0,1]
Output: 2
Example 2:
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
Note:
Your algorithm should run in linear runtime complexity.
Could you implement it using only constant extra space complexity?
*/
publicclassSolution {
publicintmissingNumber(int[] nums) {
// check input
if (nums == null || nums.length == 0) {
return0;
}
intn = nums.length;
// 1st loop, swap to correct location
for (inti = 0; i < n; i++) {
intval = nums[i];
while (val != i && val < n && val != nums[val]) { // val != nums[val], avoid infinitely loop
swap(nums, val, i);
val = nums[i];
}
}
// 2nd loop, find 1st missing
for (inti = 0; i < n; i++) {
if (nums[i] != i) {
returni;
}
}
returnn;
}
privatevoidswap(int[] nums, intx, inty) {
inttemp = nums[x];
nums[x] = nums[y];
nums[y] = temp;
}
}
publicclassSolution {
publicintmissingNumber(int[] nums) {
// check input
if (nums == null || nums.length == 0) {
return0;
}
intn = nums.length;
// 1st loop, swap to correct location
inti = 0;
while (i < n) {
intval = nums[i];
if (val != i && val < n && val != nums[val]) { // val != nums[val], avoid infinitely loop
swap(nums, val, i);
} else {
i++;
}
}
// 2nd loop, find 1st missing
for (i = 0; i < n; i++) {
if (nums[i] != i) {
returni;
}
}
returnn;
}
privatevoidswap(int[] nums, intx, inty) {
inttemp = nums[x];
nums[x] = nums[y];
nums[y] = temp;
}
}
// Bit manipulation
classSolution {
publicintmissingNumber(int[] nums) {
if (nums == null || nums.length == 0) {
return0;
}
intresult = nums.length;
for (inti = 0; i < nums.length; i++) {
result = result ^ nums[i] ^ i;
}
returnresult;
}
}
```