File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ def fourSum (self , nums : List [int ], target : int ) -> List [List [int ]]:
3+ results = []
4+ nums .sort ()
5+
6+ for i in range (len (nums )- 2 ):
7+ if i > 0 and nums [i ] == nums [i - 1 ]: continue
8+
9+ left1 , right = i + 1 , len (nums )- 1
10+ while left1 < right :
11+ left2 , right = left1 + 1 , len (nums )- 1
12+ while left2 < right :
13+ sum = nums [i ] + nums [left1 ] + nums [left2 ] + nums [right ]
14+ if sum < target :
15+ left2 += 1
16+ elif sum > target :
17+ right -= 1
18+ else :
19+ results .append ((nums [i ], nums [left1 ], nums [left2 ], nums [right ]))
20+ while left2 < right and nums [left2 ] == nums [left2 + 1 ]:
21+ left2 += 1
22+ while left2 < right and nums [right ] == nums [right - 1 ]:
23+ right -= 1
24+ left2 += 1
25+ right -= 1
26+ left1 += 1
27+ while nums [left1 ] == nums [left1 - 1 ] and left1 < right :
28+ left1 += 1
29+
30+ return results
You can’t perform that action at this time.
0 commit comments