LeetCode 1. Two Sum

개발공부를해보자·2025년 1월 7일

LeetCode

목록 보기
7/95

파이썬 알고리즘 인터뷰 문제 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로 풀고, 해답을 보고 나서 엄청난 감동을 받았었다.
책에 다른 풀이도 몇 개 있으나 유의미하지 않다고 판단하여 쓰지 않는다.

profile
개발 공부하는 30대 비전공자 직장인

0개의 댓글