From 5fadc9e31d549beace1f025a8ac39ac3f135ba46 Mon Sep 17 00:00:00 2001 From: ist187644 Date: Sat, 23 Oct 2021 11:14:52 +0100 Subject: [PATCH] Added solution for 15. 3Sum Leetcode problem --- LeetCode/Problems/Python/15_3Sum.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 LeetCode/Problems/Python/15_3Sum.py 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