From 9315212e221ce7f24b52158f365d6e74b779f9a9 Mon Sep 17 00:00:00 2001 From: Deeksha Jagtap <67088622+deek121477@users.noreply.github.com> Date: Wed, 6 Oct 2021 12:07:49 +0530 Subject: [PATCH] Added README and 3Sum files --- 0015/3Sum.py | 17 +++++++++++++++++ 0015/README.md | 30 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 0015/3Sum.py create mode 100644 0015/README.md 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