Skip to content

Instantly share code, notes, and snippets.

@PttCodingMan
Created May 18, 2022 05:56
Show Gist options
  • Save PttCodingMan/52d3cfe6cb32b82410ae7e4a6a26e934 to your computer and use it in GitHub Desktop.
Save PttCodingMan/52d3cfe6cb32b82410ae7e4a6a26e934 to your computer and use it in GitHub Desktop.
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if (length := len(nums)) < 3:
return []
result_map = {}
result = []
targets = []
for i in range(length):
target = nums[i]
if target in targets:
continue
targets.append(target)
# target + nums[j] = value_map[-target - nums[j]]
value_map = {}
for j in range(i + 1, length):
if (v := -target - nums[j]) in value_map:
current_result = sorted([nums[i], v, nums[j]])
if current_result not in result:
result.append(current_result)
value_map[nums[j]] = j
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment