Skip to content

Commit 60ef283

Browse files
committed
rivkode missing number
1 parent 2902a5d commit 60ef283

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
1. ๋ฌธ์ œ ์ดํ•ด
3+
nums ๊ฐ€ n ๊ฐœ ์ฃผ์–ด์กŒ์„๋•Œ 0๋ถ€ํ„ฐ n ๊นŒ์ง€์˜ ์ˆซ์ž์ค‘ ๋น ์ง„ ์ˆซ์ž๋ฅผ ๋ฆฌํ„ด
4+
5+
2. ์•Œ๊ณ ๋ฆฌ์ฆ˜
6+
์ •๋ ฌ nlogn ํ›„ for๋ฌธ์„ ๋Œ๊ณ  1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ์•„๋‹ ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฒˆํ˜ธ ๋ฐ˜ํ™˜ -> ์ด๊ฑฐ ๋ง๊ณ  set ์‚ฌ์šฉ
7+
8+
set ์œผ๋กœ ์ˆซ์ž ๋„ฃ๊ณ  contains๋กœ ์ฐพ์•„์„œ ์ง„ํ–‰
9+
10+
3. ์˜ˆ์™ธ
11+
12+
4. ๊ตฌํ˜„
13+
14+
array ์ •๋ ฌ
15+
1์”ฉ ์ฆ๊ฐ€
16+
์ฐพ๊ธฐ
17+
18+
์ถ”๊ฐ€ ํŒ”๋กœ์—… -> o(n) ์—์„œ ๊ฐ€๋Šฅํ•œ๊ฐ€ ?
19+
20+
set ์‚ฌ์šฉ ?
21+
key ๋ฅผ ํ†ตํ•ด ํ•ด๋‹น key ๊ฐ€ ์กด์žฌํ•˜๋ฉด ๊ณ„์† for ๋ฌธ ์ง„ํ–‰
22+
์—†๋‹ค๋ฉด ํ•ด๋‹น ์ˆซ์ž ๋ฐ˜ํ™˜
23+
์–ด์ฐจํ”ผ SET ๋„ map ์‚ฌ์šฉํ•จ
24+
์ •๋ ฌ ์—†์œผ๋ฏ€๋กœ o(n) ์œผ๋กœ ํƒ์ƒ‰ ๊ฐ€๋Šฅ set.contains ๋Š” O(1)
25+
26+
*/
27+
28+
import java.util.*;
29+
30+
class Solution {
31+
public int missingNumber(int[] nums) {
32+
Set<Integer> set = new HashSet<>();
33+
34+
for (int i: nums) {
35+
set.add(i);
36+
}
37+
38+
for (int i=0; i<nums.length; i++) {
39+
if (set.contains(i)) {
40+
continue;
41+
}
42+
return i;
43+
}
44+
45+
return nums.length;
46+
}
47+
}
48+

0 commit comments

Comments
ย (0)