알고리즘/Leetcode

[leetcode/python3] 1. Two Sum

llollhh_ 2021. 8. 3. 23:46

https://leetcode.com/problems/two-sum/

 

Two Sum - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        res = []
        dicts = {}
        
        for i in range(len(nums)):
            if (target - nums[i]) in dicts:
                value = dicts[target - nums[i]]
                res.append(value)
                res.append(i)
            else:
                dicts[nums[i]] = i
            
        return res