| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 해커랭크
- ProblemSolving
- Code Jam 2022
- Kubernetes
- Python
- 프로그래머스
- on-prem
- 하늘과 바람과 별과 시
- 3D PRINTING
- leetcode
- K8S
- LEVEL 2
- Qualification Round
- swift
- Algorithm
- 하늘과 바람과 별과 詩
- 파이썬
- MySQL
- 문제해결
- 알고리즘
- Code Jam
- Certbot/dns-route53
- 리트코드
- GitLab
- ingress-nginx
- hackerrank
- C++
- 2022
- 코딩테스트
Archives
- Today
- Total
공대생의 비망록
[LeetCode][Easy] Missing Number Python 풀이 본문
Programming Language/Python
[LeetCode][Easy] Missing Number Python 풀이
myungsup1250 2026. 2. 8. 01:35이 문제는 보자마자 Python에 익숙한 사람이라면 누구나 쉽게 풀 수 있는 문제라는 생각이 들었다.
for-loop을 통해 0부터 n까지 하나씩 확인하는 방법: (시간복잡도 O(n)이나 비효율적인 편)
class Solution:
def missingNumber(self, nums: List[int]) -> int:
for i in range(len(nums) + 1):
if i not in nums:
return i
set을 사용하며 쉽고 빠르게 문제를 해결하는 방법:
class Solution:
def missingNumber(self, nums: List[int]) -> int:
comp = set(range(len(nums) + 1))
missing = comp - set(nums)
return missing.pop()
그 외에도 본 문제를 해결하는 방법에는 가우스 공식 (n * n+1 / 2)을 활용하는 방법, XOR 비트연산을 활용하는 방법... 등이 있겠다.
끝!
'Programming Language > Python' 카테고리의 다른 글
| [LeetCode][Easy] Move Zeroes Python 풀이 (0) | 2026.02.08 |
|---|---|
| [LeetCode][Easy] Merge Sorted Array Python 풀이 (0) | 2026.02.07 |
| [LeetCode][Easy] Contains Duplicate 문제 Python 풀이 (0) | 2026.02.05 |
| [LeetCode][Easy] Two Sum 문제 Python 풀이 (0) | 2026.02.05 |
Comments
