Skip to content

Commit 7b273fe

Browse files
author
jinvicky
committed
missing number solution
1 parent f5fa02c commit 7b273fe

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

โ€Žhouse-robber-ii/jinvicky.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Solution {
22
/**
33
* ๊ธฐ์กด house-robber 1 ๋ฌธ์ œ์—์„œ ๋กœ์ง์„ ๊ฐ€์ ธ์˜ค๋˜, ์ ‘๊ทผ๋ฒ•์„ ๋‹ฌ๋ฆฌํ•˜๋Š” ๋ฌธ์ œ
4+
* ์ฒ˜์Œ์ƒ๊ฐํ–ˆ๋˜ ์ ‘๊ทผ๋ฒ•์€ ๊ทธ๋ƒฅ dp๊ฐ’์—์„œ ์ฒซ๋ฒˆ์งธ ๋˜๋Š” ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋นผ์„œ ๋‚˜์˜จ ์ตœ๋Œ“๊ฐ’ ์•„๋‹Œ๊ฐ€? ํ–ˆ์ง€๋งŒ ๋ฒ”์œ„์— ๋”ฐ๋ผ dp๊ฐ€ ๋‹ฌ๋ผ์ง€๋ฏ€๋กœ ์˜ค๋‹ต
45
*/
56
public int rob(int[] nums) {
67
if (nums.length == 1) return nums[0];

โ€Žmissing-number/jinvicky.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
/**
3+
* ์ฒ˜์Œ์—๋Š” set ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋™์›ํ•ด์„œ ๊ผญ ๋น ์ง„ ์ˆซ์ž๋ฅผ ์ฐพ๊ฒ ๋‹ค๊ณ  ๋‹ค์งํ–ˆ์œผ๋‚˜,
4+
* ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๋‹จ์ˆœํžˆ ๋ฒ”์œ„๊ฐ€ 0๋ถ€ํ„ฐ nums.length๊นŒ์ง€์˜ ์—ฐ์†๋œ ์‹œํ€€์Šค๋ผ๋ฉด
5+
* ๊ทธ๋ƒฅ 0๋ถ€ํ„ฐ n๊นŒ์ง€ ๋”ํ–ˆ์„ ๋•Œ์˜ ์›๋ž˜ ์˜ˆ์ƒ๊ฐ’์—์„œ ํ˜„์žฌ nums์˜ ํ•ฉ๊ณ„๋ฅผ ๋นผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.
6+
*
7+
* ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ• ๋•Œ์™€ ๋น„์Šทํ•˜๊ฒŒ ๊ตณ์ด ๋‚ด์šฉ ์•ˆ์„ ๋‹ค ์ฐพ์œผ๋ ค๊ณ  ํ˜•์‹ ์ž๋ฃŒ๊ตฌ์กฐ์— ์–ฝ๋งค์ด์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
8+
*/
9+
public int missingNumber(int[] nums) {
10+
int expected = 0; // 0๋ถ€ํ„ฐ n๊นŒ์ง€ ๋”ํ•œ ์ˆซ์ž์˜ ํ•ฉ๊ณ„
11+
int input = 0; //nums๊ฐ€ ์ค€ ์ˆซ์ž๋“ค์˜ ํ•ฉ๊ณ„
12+
13+
for (int n : nums) {
14+
input += n;
15+
}
16+
17+
for (int i = 0; i <= nums.length; i++) {
18+
expected += i;
19+
}
20+
// System.out.println(expected + " and " + input);
21+
22+
return expected - input;
23+
}
24+
}

0 commit comments

Comments
ย (0)