MCPcopy Create free account
hub / github.com/Hsinha11/Leetcode-solutions / fourSum

Method fourSum

18-4sum/4sum.py:2–26  ·  view source on GitHub ↗
(self, nums: List[int], target: int)

Source from the content-addressed store, hash-verified

1class Solution:
2 def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
3 n = len(nums)
4 nums.sort()
5 ans=[]
6 for i in range(n):
7 if i>0 and nums[i]==nums[i-1]:
8 continue
9 for j in range(i+1,n):
10 if (j!=i+1 and nums[j]==nums[j-1]):continue
11 k = j+1
12 l = n-1
13 while k<l:
14 sum = nums[i] + nums[j] + nums[k] + nums[l]
15 if sum ==target:
16 li = [nums[i],nums[j],nums[k],nums[l]]
17 ans.append(li)
18 k+=1
19 l-=1
20 while(k<l and nums[k]==nums[k-1]):k+=1
21 while(k<l and nums[l]==nums[l+1]):l-=1
22 elif sum>target:
23 l-=1
24 elif sum<target:
25 k+=1
26 return ans

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected