| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 파이썬
- 3D PRINTING
- C++
- on-prem
- secondlowestgrade
- 코딩테스트
- 하늘과 바람과 별과 시
- Code Jam
- Code Jam 2022
- Kubernetes
- 하늘과 바람과 별과 詩
- 알고리즘
- hackerrank
- LEVEL 2
- Certbot/dns-route53
- ingress-nginx
- MySQL
- Algorithm
- 프로그래머스
- openebs
- nestedlists
- K8S
- swift
- leetcode
- Qualification Round
- 해커랭크
- GitLab
- Python
- 2022
Archives
- Today
- Total
공대생의 비망록
[LeetCode][Easy] Contains Duplicate 문제 Python 풀이 본문
Programming Language/Python
[LeetCode][Easy] Contains Duplicate 문제 Python 풀이
myungsup1250 2026. 2. 5. 15:34중첩 for-loop 활용 방법: <O(n^2)>
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] == nums[j]:
return True
return False
집합 (Set) 자료구조 활용 방법: <O(n)>
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
exists = set()
for num in nums:
if num in exists:
return True
else:
exists.add(num)
return False
보다 Python 스러운 방법 (The most Pythonic way): <O(n)> - 시간복잡도는 위의 방법과 같이 O(n)이나 중복 원소가 조기에 확인되는 경우 본 방법보다 빠를 수 있음!
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums)) # Most pythonic way to solve this problem.'Programming Language > Python' 카테고리의 다른 글
| [LeetCode][Easy] Two Sum 문제 Python 풀이 (0) | 2026.02.05 |
|---|
Comments
