We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a409297 commit 788967cCopy full SHA for 788967c
1 file changed
โmissing-number/se6816.javaโ
@@ -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