Skip to content

Latest commit

 

History

History
37 lines (33 loc) · 819 Bytes

001.md

File metadata and controls

37 lines (33 loc) · 819 Bytes

1. Two Sum

Solution 1 (O(n))

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        d = {}
        for i, num in enumerate(nums):
            t = target - num
            if t in d:
                return [d[t], i]
            d[num] = i
        return []

Solution 2 (O(n^2))

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                if nums[i] + nums[j] == target:
                    return [i, j]
        return []