Skip to content

Commit b1ee6db

Browse files
Merge branch 'main' of https://github.com/ShrujanKotturi/leetcode into main
2 parents 152aa04 + 41a53fd commit b1ee6db

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: python/18-4Sum.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def fourSum(self, nums, target):
3+
def findNsum(l, r, target, N, result, results):
4+
if r-l+1 < N or N < 2 or target < nums[l]*N or target > nums[r]*N:
5+
return
6+
if N == 2:
7+
while l < r:
8+
s = nums[l] + nums[r]
9+
if s == target:
10+
results.append(result + [nums[l], nums[r]])
11+
l += 1
12+
while l < r and nums[l] == nums[l-1]:
13+
l += 1
14+
elif s < target:
15+
l += 1
16+
else:
17+
r -= 1
18+
else:
19+
for i in range(l, r+1):
20+
if i == l or (i > l and nums[i-1] != nums[i]):
21+
findNsum(i+1, r, target-nums[i], N-1, result+[nums[i]], results)
22+
23+
nums.sort()
24+
results = []
25+
findNsum(0, len(nums)-1, target, 4, [], results)
26+
return results

0 commit comments

Comments
 (0)