| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
| 29 | 30 | 31 |
Tags
- 해커랭크
- 하늘과 바람과 별과 詩
- Kubernetes
- ProblemSolving
- 2022
- 프로그래머스
- K8S
- C++
- 하늘과 바람과 별과 시
- Count Monobit Integers
- swift
- 리트코드
- Python
- 문제해결
- First Unique Character in a String
- LEVEL 2
- 파이썬
- Algorithm
- leetcode
- Code Jam
- hackerrank
- ProblemSoving
- 3D PRINTING
- 코딩테스트
- GitLab
- Qualification Round
- MySQL
- 알고리즘
- Code Jam 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] Single Number 문제 Python 풀이 (0) | 2026.02.08 |
|---|---|
| [LeetCode][Easy] Missing Number 문제 Python 풀이 (0) | 2026.02.08 |
| [LeetCode][Easy] Move Zeroes 문제 Python 풀이 (0) | 2026.02.08 |
| [LeetCode][Easy] Merge Sorted Array 문제 Python 풀이 (0) | 2026.02.07 |
| [LeetCode][Easy] Two Sum 문제 Python 풀이 (0) | 2026.02.05 |
Comments