diff --git a/3 Sum.py b/3 Sum.py new file mode 100644 index 00000000..7ce904bf --- /dev/null +++ b/3 Sum.py @@ -0,0 +1,37 @@ +#time complexity -0(n^2) and space complexity - o(1) +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + result=[] + nums.sort() + n=len(nums) + + for i in range (n-2): + if i!=0 and nums[i]==nums[i-1]: + continue + if nums[i]>0: + break + l= i+1 + h=n-1 + while l< h : + sum = nums[i]+nums[l]+nums[h] + + if sum== 0: + result.append([nums[i],nums[l],nums[h]]) + + l= l+1 + h= h-1 + + while (l int: + result =0 + + l= 0 + h=len(height)-1 + + while l None: + if nums==None or len(nums)==0: + return + n= len(nums) + low=0 # storing 0 + high= n-1 #storing 2 + mid = 0 # storing 1 + + while mid<=high: + if nums[mid]==0: + nums[mid],nums[low]= nums[low], nums[mid] + low=low+1 + mid=mid+1 + elif nums[mid]==1: + mid = mid+1 + else: + nums[mid],nums[high]=nums[high], nums[mid] + high = high-1 + \ No newline at end of file