forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjinvicky.java
More file actions
24 lines (21 loc) ยท 931 Bytes
/
jinvicky.java
File metadata and controls
24 lines (21 loc) ยท 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
/**
* ์ฒ์์๋ set ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋์ํด์ ๊ผญ ๋น ์ง ์ซ์๋ฅผ ์ฐพ๊ฒ ๋ค๊ณ ๋ค์งํ์ผ๋,
* ์๊ฐํด๋ณด๋ ๋จ์ํ ๋ฒ์๊ฐ 0๋ถํฐ nums.length๊น์ง์ ์ฐ์๋ ์ํ์ค๋ผ๋ฉด
* ๊ทธ๋ฅ 0๋ถํฐ n๊น์ง ๋ํ์ ๋์ ์๋ ์์๊ฐ์์ ํ์ฌ nums์ ํฉ๊ณ๋ฅผ ๋นผ๋ฉด ๋๋ ๊ฒ์ด๋ค.
*
* ์ต๋๊ฐ, ์ต์๊ฐ์ ๊ตฌํ ๋์ ๋น์ทํ๊ฒ ๊ตณ์ด ๋ด์ฉ ์์ ๋ค ์ฐพ์ผ๋ ค๊ณ ํ์ ์๋ฃ๊ตฌ์กฐ์ ์ฝ๋งค์ด์ง ์์๋ ๋๋ค.
*/
public int missingNumber(int[] nums) {
int expected = 0; // 0๋ถํฐ n๊น์ง ๋ํ ์ซ์์ ํฉ๊ณ
int input = 0; //nums๊ฐ ์ค ์ซ์๋ค์ ํฉ๊ณ
for (int n : nums) {
input += n;
}
for (int i = 0; i <= nums.length; i++) {
expected += i;
}
// System.out.println(expected + " and " + input);
return expected - input;
}
}