forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclara-shin.js
More file actions
29 lines (26 loc) ยท 866 Bytes
/
clara-shin.js
File metadata and controls
29 lines (26 loc) ยท 866 Bytes
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
/**
* n๊ฐ์ ์๋ก ๋ค๋ฅธ ์ซ์๊ฐ ๋ค์ด์๋ ๋ฐฐ์ด์์, 0๋ถํฐ n๊น์ง์ ๋ฒ์ ์ค ๋น ์ง ์ซ์ ํ๋๋ฅผ ์ฐพ๋ ๋ฌธ์
* ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ n์ด๋ฉด, ์ค์ ๋ก๋ 0๋ถํฐ n๊น์ง ์ด (n+1)๊ฐ์ ์ซ์ ์ค ํ๋๊ฐ ๋น ์ ธ์์
*
* ์ ๊ทผ๋ฐฉ๋ฒ: XOR ๋นํธ ์ฐ์ฐ
* ์๊ฐ ๋ณต์ก๋: O(n)
* ๊ณต๊ฐ ๋ณต์ก๋: O(1)
*
* XOR์ ์ฑ์ง:
* - a ^ a = 0 (๊ฐ์ ์๋ผ๋ฆฌ XORํ๋ฉด 0)
* - a ^ 0 = a (์ด๋ค ์๋ 0๊ณผ XORํ๋ฉด ์๊ธฐ ์์ )
* - XOR์ ๊ตํ๋ฒ์น๊ณผ ๊ฒฐํฉ๋ฒ์น์ด ์ฑ๋ฆฝ
*/
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
let result = nums.length; // n์ผ๋ก ์ด๊ธฐํ
// ์ธ๋ฑ์ค i์ nums[i]๋ฅผ ๋ชจ๋ XOR
// ๊ฒฐ๊ตญ ๋น ์ง ์ซ์๋ง ํ์ ๋ฒ ๋ํ๋์ ๊ฒฐ๊ณผ๋ก ๋จ์
for (let i = 0; i < nums.length; i++) {
result ^= i ^ nums[i];
}
return result;
};