Skip to content

Commit f805dc5

Browse files
committed
Solution Week11: missing-number
1 parent 2283096 commit f805dc5

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

missing-number/juhui-jeong.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도: O(1)
4+
*/
5+
class Solution {
6+
public int missingNumber(int[] nums) {
7+
int n = nums.length;
8+
long expected = n * (n + 1) / 2;
9+
long actual = 0;
10+
for (int x : nums) {
11+
actual += x;
12+
}
13+
return (int) (expected - actual);
14+
}
15+
}
16+
17+
/*
18+
* 시간 복잡도: O(n log n)
19+
* 공간 복잡도: O(1)
20+
*/
21+
class Solution {
22+
public int missingNumber(int[] nums) {
23+
Arrays.sort(nums);
24+
for (int i = 0; i < nums.length; i++) {
25+
if (i != nums[i]) {
26+
return i;
27+
}
28+
}
29+
return nums.length;
30+
}
31+
}

0 commit comments

Comments
 (0)