This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
NewerOlder