diff --git a/LeetCode/Problems/Python/15_3Sum.py b/LeetCode/Problems/Python/15_3Sum.py new file mode 100644 index 00000000..f8b76d9d --- /dev/null +++ b/LeetCode/Problems/Python/15_3Sum.py @@ -0,0 +1,18 @@ +class Solution: + def threeSum(self, nums): + res = set() + targets_dict = {} + + nums.sort() + + for i, num in enumerate(nums): + targets_dict = {} + target = -num + if(i>0 and nums[i] == nums[i-1]): + continue + for num2 in nums[i+1:]: + if(num2 in targets_dict): + res.add((num, num2, targets_dict[num2])) + targets_dict[target-num2] = num2 + + return res \ No newline at end of file