| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- MySQL
- Qualification Round
- 알고리즘
- GitLab
- 2022
- Count Monobit Integers
- swift
- 프로그래머스
- hackerrank
- ProblemSoving
- Algorithm
- 해커랭크
- 하늘과 바람과 별과 시
- ProblemSolving
- 문제해결
- Kubernetes
- 코딩테스트
- 하늘과 바람과 별과 詩
- Code Jam 2022
- 파이썬
- Python
- Code Jam
- First Unique Character in a String
- leetcode
- K8S
- 리트코드
- LEVEL 2
- C++
- 3D PRINTING
Archives
- Today
- Total
공대생의 비망록
[LeetCode][Easy] Two Sum 문제 Python 풀이 본문
중첩 for-loop 사용: <O(n^2)>
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range (i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
시간복잡도 개선을 위해 Dictionary 자료구조를 활용하는 방식: <O(n)>
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
desiredTarget = {}
for i in range(len(nums)):
if nums[i] in desiredTarget: # is not empty
return [desiredTarget[nums[i]], i]
else:
desiredTarget[target - nums[i]] = i'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] Contains Duplicate 문제 Python 풀이 (0) | 2026.02.05 |
Comments