Skip to content

Commit f56cf9e

Browse files
author
แ„‹แ…ตแ„‹แ…งแ†ซแ„‰แ…ฎ
committed
missing number
1 parent cf5e717 commit f56cf9e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode_study
2+
3+
/*
4+
* ์ฃผ์–ด์ง„ ์ˆซ์ž ๋ฐฐ์—ด์—์„œ ์กด์žฌํ•˜์ง€ ์•Š์€ ์ˆ˜ ํŒ๋ณ„ํ•˜๋Š” ๋ฌธ์ œ
5+
* 0๋ถ€ํ„ฐ n์˜ ๋ฒ”์œ„์—์„œ ๋ชจ๋“  ์ˆ˜๋Š” ์œ ์ผํ•˜๊ฒŒ ์กด์žฌ
6+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
7+
* -> nums ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ, ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด board ๋ฐฐ์—ด์— ๊ฐ’์„ ์„ค์ •: O(n)
8+
* -> board ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ, ๊ฐ’์ด 0์ธ ์ธ๋ฑ์Šค ๊ฒ€์ƒ‰: O(n)
9+
* --> O(n) + O(n) = O(n)
10+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
11+
* -> ์ž…๋ ฅ ๋ฐฐ์—ด nums์˜ ํฌ๊ธฐ๊ฐ€ n์ผ ๋•Œ, board ๋ฐฐ์—ด์˜ ํฌ๊ธฐ n + 1: O(n)
12+
* */
13+
fun missingNumber(nums: IntArray): Int {
14+
val size = nums.size
15+
val board = IntArray(size+1)
16+
for (index in nums.indices) {
17+
board[nums[index]] = 1
18+
}
19+
20+
for (i in board.indices) {
21+
if (board[i] == 0) {
22+
return i
23+
}
24+
}
25+
return 0
26+
}

0 commit comments

Comments
ย (0)