-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-268-Missing-Number.java
70 lines (58 loc) · 1.91 KB
/
LeetCode-268-Missing-Number.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
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
/*
http://www.cnblogs.com/grandyang/p/4756677.html
[3,0,1] - 2
[1,2] - 0
[2,1] - 0
[1,0] - 2
*/
class Solution {
// 1. Filling the nums to the correct location in the array
// public int missingNumber(int[] nums) {
// for (int i = 0; i < nums.length; ) {
// if (nums[i] >= nums.length) {
// nums[i] = -1;
// i++;
// } else if (nums[i] == i || nums[i] == -1) {
// i++;
// } else {
// // nums[i] is not in the correct location
// swap(nums, i, nums[i]); // swap nums in i, and nums[i]
// }
// }
// for (int i = 0; i < nums.length; i++) {
// if (nums[i] != i) return i;
// }
// return nums[nums.length - 1] + 1;
// }
// private void swap(int[] nums, int i, int j) {
// int temp = nums[i];
// nums[i] = nums[j];
// nums[j] = temp;
// }
// 2. sum{0,...,n} - sum(nums[0],...,nums[n-1])
/*
https://leetcode.com/problems/missing-number/discuss/69795/Java-solution-O(1)-space-and-O(n)-in-time
*/
// public int missingNumber(int[] nums) {
// int sum = 0;
// for (int n : nums) sum += n;
// return nums.length * (nums.length + 1) / 2 - sum;
// }
// 3.XOR
/*
https://leetcode.com/problems/missing-number/discuss/69791/4-Line-Simple-Java-Bit-Manipulate-Solution-with-Explaination
a^b^b=a
idx {0, n-1}
nums[i] {0,n} but missed one
so we could make a number XOR to all index {0, n}, and then XOR to all nums[0, n-1]
Assume value is k
So the process will be: k^0^0^1^1^...^n^n, finally the result is k
*/
public int missingNumber(int[] nums) {
int res = nums.length;
for (int i = 0; i < nums.length; i++) {
res = res^i^nums[i];
}
return res;
}
}