sy1214ei 님의 블로그

[Leet Code] 1. Two Sum - Python 본문

[Coding]

[Leet Code] 1. Two Sum - Python

sy1214ei 2024. 12. 3. 00:25

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

Level: Easy
Topics: Array | Hash Table
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        """
        1. for i, num in enumerate(nums) : 뭔가 순회를 해야 찾든말든 할것임
        2. 우리가 찾는 것 find = target - num
        3. 만약 hash_table에 find가 있다면 return
        4. hash_table[num] = i
        """
        hash_table = {}
        for i, num in enumerate(nums):
            find = target - num
            if find in hash_table:
                return [hash_table[find], i]
            hash_table[num] = i

		# Time Complexity : O(n)
            	# Space Complexity : O(n)