File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * n๊ฐ์ ์๋ก ๋ค๋ฅธ ์ซ์๊ฐ ๋ค์ด์๋ ๋ฐฐ์ด์์, 0๋ถํฐ n๊น์ง์ ๋ฒ์ ์ค ๋น ์ง ์ซ์ ํ๋๋ฅผ ์ฐพ๋ ๋ฌธ์
3
+ * ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ n์ด๋ฉด, ์ค์ ๋ก๋ 0๋ถํฐ n๊น์ง ์ด (n+1)๊ฐ์ ์ซ์ ์ค ํ๋๊ฐ ๋น ์ ธ์์
4
+ *
5
+ * ์ ๊ทผ๋ฐฉ๋ฒ: XOR ๋นํธ ์ฐ์ฐ
6
+ * ์๊ฐ ๋ณต์ก๋: O(n)
7
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(1)
8
+ *
9
+ * XOR์ ์ฑ์ง:
10
+ * - a ^ a = 0 (๊ฐ์ ์๋ผ๋ฆฌ XORํ๋ฉด 0)
11
+ * - a ^ 0 = a (์ด๋ค ์๋ 0๊ณผ XORํ๋ฉด ์๊ธฐ ์์ )
12
+ * - XOR์ ๊ตํ๋ฒ์น๊ณผ ๊ฒฐํฉ๋ฒ์น์ด ์ฑ๋ฆฝ
13
+ */
14
+
15
+ /**
16
+ * @param {number[] } nums
17
+ * @return {number }
18
+ */
19
+ var missingNumber = function ( nums ) {
20
+ let result = nums . length ; // n์ผ๋ก ์ด๊ธฐํ
21
+
22
+ // ์ธ๋ฑ์ค i์ nums[i]๋ฅผ ๋ชจ๋ XOR
23
+ // ๊ฒฐ๊ตญ ๋น ์ง ์ซ์๋ง ํ์ ๋ฒ ๋ํ๋์ ๊ฒฐ๊ณผ๋ก ๋จ์
24
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
25
+ result ^= i ^ nums [ i ] ;
26
+ }
27
+
28
+ return result ;
29
+ } ;
You canโt perform that action at this time.
0 commit comments