Skip to content

Commit 16977f7

Browse files
committed
missing-number
1 parent 6a63316 commit 16977f7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

โ€Žmissing-number/taekwon-dev.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
3+
* - ๊ณต์ฐจ๊ฐ€ 1์ธ ๋“ฑ์ฐจ์ˆ˜์—ด, ๋“ฑ์ฐจ์ˆ˜์—ด์˜ ํ•ฉ ๊ณต์‹ ํ™œ์šฉํ•˜์—ฌ ๊ธฐ๋Œ€ ๊ฐ’์„ ๊ณ„์‚ฐ -> O(1)
4+
* - ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ์›์†Œ์˜ ํ•ฉ์„ ๊ณ„์‚ฐ -> O(n)
5+
* - ๊ธฐ๋Œ€ ๊ฐ’์—์„œ ์‹ค์ œ ๊ฐ ์›์†Œ์˜ ํ•ฉ์„ ๋นผ๋ฉด ์ •๋‹ต -> O(1)
6+
*
7+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
8+
*
9+
*/
10+
class Solution {
11+
public int missingNumber(int[] nums) {
12+
int len = nums.length;
13+
int expectedSum = len * (len + 1) / 2;
14+
int actualSum = 0;
15+
16+
for (int num: nums) {
17+
actualSum += num;
18+
}
19+
20+
return expectedSum - actualSum;
21+
}
22+
}

0 commit comments

Comments
ย (0)