diff --git a/0015/3Sum.py b/0015/3Sum.py new file mode 100644 index 0000000..eec4134 --- /dev/null +++ b/0015/3Sum.py @@ -0,0 +1,17 @@ +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + ans = set() + for i in range(len(nums) - 2): + j = i + 1 + k = len(nums) - 1 + while k > j: + s = nums[i] + nums[j] + nums[k] + if s == 0: + ans.add((nums[i], nums[j], nums[k])) + j += 1 + elif s < 0: + j += 1 + else: + k -= 1 + return list(ans) \ No newline at end of file diff --git a/0015/README.md b/0015/README.md new file mode 100644 index 0000000..e3d690f --- /dev/null +++ b/0015/README.md @@ -0,0 +1,30 @@ +# 3Sum +Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. + +Notice that the solution set must not contain duplicate triplets. + + + +## Example 1: + +Input: nums = [-1,0,1,2,-1,-4] + +Output: [[-1,-1,2],[-1,0,1]] + +## Example 2: + +Input: nums = [] + +Output: [] + +## Example 3: + +Input: nums = [0] + +Output: [] + + +## Constraints: + +0 <= nums.length <= 3000 +-105 <= nums[i] <= 105 \ No newline at end of file