(self, nums: List[int])
| 1 | # Three Sum dynamic problem solution |
| 2 | def threeSum(self, nums: List[int]) -> List[List[int]]: |
| 3 | res = [] |
| 4 | nums.sort() |
| 5 | |
| 6 | for i, num1 in enumerate(nums): |
| 7 | |
| 8 | if i > 0 and num1 == nums[i-1]: |
| 9 | continue |
| 10 | |
| 11 | start, end = i+1, len(nums) - 1 |
| 12 | |
| 13 | while start < end: |
| 14 | sum = num1 + nums[start] + nums[end] |
| 15 | |
| 16 | if sum > 0: |
| 17 | end -= 1 |
| 18 | elif sum < 0: |
| 19 | start += 1 |
| 20 | else: |
| 21 | res.append([num1, nums[start], nums[end]]) |
| 22 | start += 1 |
| 23 | while nums[start] == nums[start-1] and start < end: |
| 24 | start += 1 |
| 25 | |
| 26 | return res |