Skip to content

Commit c40ce20

Browse files
committed
Solution: Missing Number
1 parent 138a294 commit c40ce20

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

โ€Žmissing-number/flynn.cppโ€Ž

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* ํ’€์ด
3+
* - ํŠน์ • ์ •์ˆ˜๊ฐ€ `nums` ๋ฐฐ์—ด์„ ํ†ตํ•ด ์ฃผ์–ด์กŒ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๋Š” ๋ฐฐ์—ด `check`๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค
4+
* - `nums`๋ฅผ ์กฐํšŒํ•˜๋ฉฐ `check`๋ฐฐ์—ด์˜ ๊ฐ’๋“ค์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค
5+
* - `check`๋ฐฐ์—ด์„ ์กฐํšŒํ•˜์—ฌ ๋ˆ„๋ฝ๋˜์—ˆ๋˜ ์ •์ˆ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค
6+
*
7+
* Big-O
8+
* - N: ์ฃผ์–ด์ง„ ๋ฐฐ์—ด `nums`์˜ ํฌ๊ธฐ
9+
*
10+
* - Time Complexity: O(N)
11+
* - ๋ฐฐ์—ด `nums`๋ฅผ ์กฐํšŒํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ์€ O(N)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค
12+
* - ๋ฐฐ์—ด `check`๋ฅผ ์กฐํšŒํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ ๋˜ํ•œ O(N)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค
13+
* - ๋”ฐ๋ผ์„œ ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N)์ž…๋‹ˆ๋‹ค
14+
*
15+
* - Space Complexity: O(N)
16+
* - `check`๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ์ž…๋ ฅ๊ฐ’์— ๋น„๋ก€ํ•˜์—ฌ ์„ ํ˜•์ ์œผ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค
17+
*/
18+
19+
class Solution {
20+
public:
21+
int missingNumber(vector<int>& nums) {
22+
int n = nums.size();
23+
24+
vector<bool> check(n + 1, false);
25+
26+
for (auto num : nums) check[num] = true;
27+
28+
int res;
29+
for (int i = 0; i < n + 1; i++) {
30+
if (!check[i]) {
31+
res = i;
32+
break;
33+
}
34+
}
35+
36+
return res;
37+
}
38+
};

0 commit comments

Comments
ย (0)