forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJeehay28.js
More file actions
68 lines (52 loc) · 1.51 KB
/
Jeehay28.js
File metadata and controls
68 lines (52 loc) · 1.51 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
/**
* @param {number[]} nums
* @return {number}
*/
// *** Guided approach 2: bitwise operations and avoids potential overflow issues with very large sums
// XOR method
// Time complexity: O(n)(two loops: one for numbers 0 to n and one for array elements)
// Space complexity: O(1)
var missingNumber = function (nums) {
// XOR with itself results in 0 : a xor a = 0
// XOR with 0 results in the number itself : a xor 0 = a
// XOR is commutative and associative
const n = nums.length;
let xor = 0;
for (let i = 0; i <= n; i++) {
xor ^= i;
}
for (any of nums) {
xor ^= any;
}
return xor;
};
// *** Guided approach 1: simplicity and clarity
// Gauss' Formula (Sum of First n Numbers): n*(n+1) / 2
// Time complexity: O(n)
// Space complexity: O(1)
// var missingNumber = function (nums) {
// const n = nums.length;
// const expectedSum = (n * (n + 1)) / 2;
// const actualSum = nums.reduce((acc, cur) => acc + cur, 0); // O(n)
// const missingNum = expectedSum - actualSum;
// return missingNum;
// };
// *** My own approach
// Time complexity: O(n^2)
// Space complexity: O(n)
// var missingNumber = function (nums) {
// let distinctNums = new Set([]);
// for (any of nums) {
// if (distinctNums.has(any)) {
// return
// } else {
// distinctNums.add(any)
// }
// }
// const n = distinctNums.size;
// for (let i = 0; i <= n; i++) {
// if (!nums.includes(i)) {
// return i;
// }
// }
// };