Skip to content

Commit 788967c

Browse files
committed
missing number
1 parent a409297 commit 788967c

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

โ€Žmissing-number/se6816.javaโ€Ž

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
์ตœ๋Œ“๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐฐ์—ด์˜ ํ•ฉ๊ณผ 1~N๊นŒ์ง€์˜ ํ•ฉ์„ ๋น„๊ตํ•˜์—ฌ ํ™•์ธํ•˜๋Š” ๋ฐฉ์‹
3+
*/
4+
class Solution {
5+
public int missingNumber(int[] nums) {
6+
int max = 0;
7+
int sum = 0;
8+
boolean existsZero = false;
9+
for(int i = 0; i < nums.length; i++) {
10+
max = Math.max(nums[i], max);
11+
sum += nums[i];
12+
if(nums[i] == 0){
13+
existsZero = true;
14+
}
15+
}
16+
17+
int result = (max * (max + 1)) / 2 - sum;
18+
19+
if(!existsZero) {
20+
result = 0;
21+
} else if(result == 0) {
22+
result = max + 1;
23+
}
24+
25+
return result;
26+
}
27+
}

0 commit comments

Comments
ย (0)