Skip to content

Commit fd4a0a0

Browse files
committed
feat: missing-number
1 parent c397bed commit fd4a0a0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

β€Žmissing-number/invidam.go.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
배타적인 κ²½μš°μ—λ§Œ 참을 좜λ ₯ν•˜λŠ” `XOR` 연산을 ν™œμš©ν•œλ‹€.
4+
# Approach
5+
<!-- Describe your approach to solving the problem. -->
6+
- 문제λ₯Ό μΉ˜ν™˜ν•΄λ³΄μž. λͺ¨λ“  μˆ˜κ°€ 2λ²ˆμ”© λ“±μž₯ν•˜κ³ , ν•˜λ‚˜μ˜ 수만 ν•œ 번 λ“±μž₯ν•œλ‹€κ³  ν•˜μž.
7+
- 이 경우, λͺ¨λ“  μˆ˜λ“€μ— `^`연산을 ν•˜λ©΄ ν•œ 번 λ“±μž₯ν•œ μˆ˜λ§Œμ„ μ•Œ 수 μžˆλ‹€.
8+
- `λ°°μ—΄μ˜ 길이` + `인덱슀`와 `ν•΄λ‹Ή 인덱슀일 λ•Œμ˜ μ›μ†Œκ°’`듀을 λͺ¨λ‘ `^`μ—°μ‚°ν•˜λ©΄
9+
- λ°°μ—΄ + 인덱슀 순회둜 λͺ¨λ“  μˆ˜λŠ” 1λ²ˆμ”© λ“±μž₯ν•œλ‹€.
10+
- μ›μ†Œκ°’ 순회둜 ν•˜λ‚˜μ˜ 수λ₯Ό μ œμ™Έν•˜κ³€ 1λ²ˆμ”© λ“±μž₯ν•œλ‹€.
11+
- ν•œ 번만 λ“±μž₯ν•œ μˆ˜κ°€ missing number이닀.
12+
# Complexity
13+
- Time complexity: $$O(n)$$
14+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
15+
: λ°°μ—΄μ˜ 길이 `n`, 이λ₯Ό μˆœνšŒν•œλ‹€.
16+
- Space complexity: $$O(n)$$
17+
18+
: inline, μ£Όμ–΄μ§„ λ°°μ—΄μ˜ 길이 `n`
19+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
20+
21+
# Code
22+
```go
23+
func missingNumber(nums []int) int {
24+
num := len(nums)
25+
26+
for i, n := range nums {
27+
num ^= i
28+
num ^= n
29+
}
30+
return num
31+
}
32+
```

0 commit comments

Comments
Β (0)