[Leetcode] 1. Two Sum

mydailylogs
|2023. 5. 1. 02:07
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

        complements = {}
        for i, x in enumerate(nums):
            c = target - x
            
            if x in complements:
                return [complements[x], i]
                
            complements[c] = i
        
        return [-1, -1]

'CS > 알고리즘' 카테고리의 다른 글

[Leetcode] 16. 3Sum Closest  (0) 2023.05.01
[Leetcode] 15. 3Sum  (0) 2023.05.01
[Leetcode] 12. Integer to Roman  (0) 2023.05.01
[Leetcode] 202. Happy Number  (0) 2023.04.27
[Leetcode] 55. Jump Game  (0) 2023.04.27