파이썬 알고리즘 인터뷰 문제 7번(리트코드 1번) Two Sum
https://leetcode.com/problems/two-sum/
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
remainder = dict()
for index, num in enumerate(nums):
if num in remainder:
return [remainder[num], index]
else:
remainder[target - num] = index
리트코드 1번 문제로 그 풀이가 워낙 유명하다.
물론 코딩을 아주 처음 접했을 때는 Brute Force로 풀고, 해답을 보고 나서 엄청난 감동을 받았었다.
책에 다른 풀이도 몇 개 있으나 유의미하지 않다고 판단하여 쓰지 않는다.