forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimsosleepy.java
More file actions
34 lines (30 loc) ยท 983 Bytes
/
imsosleepy.java
File metadata and controls
34 lines (30 loc) ยท 983 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
25
26
27
28
29
30
31
32
33
34
// GPT์ ๋์์ ๋ฐ์ ๊ฒฐ๊ณผ, ์ํ์ ์ผ๋ก ์ ๊ทผํ๋ฉด ๋๋ค.
// ๋ชจ๋ ๊ฐ์ ์ ๋ํฌํ๊ณ nums ๋ฐฐ์ด ์ฌ์ด์ฆ์ธ n์ ์ง์ผ์ฃผ๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํ ๊ฒฐ๊ณผ
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int expected = n * (n + 1) / 2;
int actual = 0;
for (int num : nums) {
actual += num;
}
return expected - actual;
}
}
// ์๊ฐ๋ณต์ก๋๋ O(N)์ผ๋ก ๋จ์ด์ง๋ค.
// ๊ณต๊ฐ๋ณต์ก๋๊ฐ nums ๋ฐฐ์ด ์ฌ์ด์ฆ์ ์ข
์๋์ O(N)์ด๋ค.
// Accepted๊ฐ ๋์ง๋ง, ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ฐพ์๋ด์ผํจ
class Solution {
public int missingNumber(int[] nums) {
boolean[] existCheck = new boolean[nums.length + 1];
for (int num : nums) {
existCheck[num] = true;
}
for (int i = 0; i < existCheck.length; i++) {
if (!existCheck[i]) {
return i;
}
}
return 0;
}
}