Skip to content

Instantly share code, notes, and snippets.

View ThomasHigginson's full-sized avatar

Thomas Higginson ThomasHigginson

View GitHub Profile
def threeSum(self, nums: List[int]) -> List[List[int]]:
if len(nums) < 3: # Requires 3 for a pair of 3
return []
elif len(nums) == 3 and sum(nums) == 0: # Naive case; check sum of 3 elements
return [nums]
nums = sorted(nums) # O(nlogn)
solutions = []
i = 0
def twoSum(self, nums: List[int], target: int) -> List[int]:
number2Index = {} # Lookup table for observed number to it's index
# 1. Get a given numbers complement
# 2. If it's in number2Index, then it's complement
# exists, and return it's index along with the complement's index
# 3. Otherwise, add this number as a key, and it's index as the value
for idx, num in enumerate(nums):
numComplement = target - num
if numComplement in number2Index: