|
| 1 | +from typing import List |
| 2 | +class Solution: |
| 3 | + def quicksort(l, r, obj): |
| 4 | + if not l < r: |
| 5 | + return obj |
| 6 | + i, j, x, = l, r, obj[r] |
| 7 | + while (i < j): |
| 8 | + while (i < j and obj[i] <= x): |
| 9 | + i += 1 |
| 10 | + obj[j] = obj[i] |
| 11 | + while (i < j and obj[j] > x): |
| 12 | + j -= 1 |
| 13 | + obj[i] = obj[j] |
| 14 | + obj[i] = x |
| 15 | + Solution.quicksort(l, i - 1, obj) |
| 16 | + Solution.quicksort(i + 1, r, obj) |
| 17 | + return obj |
| 18 | + def dfs(self, l1, r1): |
| 19 | + if not r1 - l1 > 2: |
| 20 | + return [] |
| 21 | + l2, r2, out = l1 + 1, r1 - 1, [] |
| 22 | + while l2 < r2: |
| 23 | + if self.nums[l1] + self.nums[l2] + self.nums[r2] + self.nums[r1] == self.target: |
| 24 | + out.append([self.nums[l1], self.nums[l2], self.nums[r2], self.nums[r1]]) |
| 25 | + l2, r2 = l2 + 1, r2 - 1 |
| 26 | + while l2 < r2 and self.nums[l2] == self.nums[l2 - 1]: |
| 27 | + l2 += 1 |
| 28 | + while l2 < r2 and self.nums[r2] == self.nums[r2 + 1]: |
| 29 | + r2 -= 1 |
| 30 | + elif self.nums[l1] + self.nums[l2] + self.nums[r2] + self.nums[r1] < self.target: |
| 31 | + l2 += 1 |
| 32 | + while l2 < r2 and self.nums[l2] == self.nums[l2 - 1]: |
| 33 | + l2 += 1 |
| 34 | + else: |
| 35 | + r2 -= 1 |
| 36 | + while l2 < r2 and self.nums[r2] == self.nums[r2 + 1]: |
| 37 | + r2 -= 1 |
| 38 | + if self.nums[l1] + self.nums[l2] + self.nums[r2] + self.nums[r1] == self.target: |
| 39 | + out += self.dfs(l1 + 1, r1) |
| 40 | + out += self.dfs(l1, r1 - 1) |
| 41 | + l1, r1 = l1 + 1, r1 - 1 |
| 42 | + while r1 - l1 > 2 and self.nums[l1] == self.nums[l1 - 1]: |
| 43 | + l1 += 1 |
| 44 | + while r1 - l1 > 2 and self.nums[r1] == self.nums[r1 + 1]: |
| 45 | + r1 -= 1 |
| 46 | + elif self.nums[l1] + self.nums[l2] + self.nums[r2] + self.nums[r1] < self.target: |
| 47 | + out += self.dfs(l1 + 1, l1 + 2, r2, r1) |
| 48 | + l1 += 1 |
| 49 | + while r1 - l1 > 2 and self.nums[l1] == self.nums[l1 - 1]: |
| 50 | + l1 += 1 |
| 51 | + else: |
| 52 | + r1 -= 1 |
| 53 | + while r1 - l1 > 2 and self.nums[r1] == self.nums[r1 + 1]: |
| 54 | + r1 -= 1 |
| 55 | + l2, r2 = l1 + 1, r1 - 1 |
| 56 | + return out |
| 57 | + def fourSum(self, nums: List[int], target: int) -> List[List[int]]: |
| 58 | + lens, self.target = len(nums), target |
| 59 | + self.nums = Solution.quicksort(0, lens - 1, nums) |
| 60 | + return self.dfs(0, 1, lens - 2, lens - 1) |
| 61 | +# l1, l2, r1, r2 = 0, 1, lens - 1, lens - 2 |
| 62 | +# while r1 - l1 > 2: |
| 63 | +# print(l1, l2, r2, r1) |
| 64 | +# while l2 < r2: |
| 65 | +# if nums[l1] + nums[l2] + nums[r2] + nums[r1] == target: |
| 66 | +# out.append([nums[l1], nums[l2], nums[r2], nums[r1]]) |
| 67 | +# l2, r2 = l2 + 1, r2 - 1 |
| 68 | +# while l2 < r2 and nums[l2] == nums[l2 - 1]: |
| 69 | +# l2 += 1 |
| 70 | +# while l2 < r2 and nums[r2] == nums[r2 + 1]: |
| 71 | +# r2 -= 1 |
| 72 | +# elif nums[l1] + nums[l2] + nums[r2] + nums[r1] < target: |
| 73 | +# l2 += 1 |
| 74 | +# while l2 < r2 and nums[l2] == nums[l2 - 1]: |
| 75 | +# l2 += 1 |
| 76 | +# else: |
| 77 | +# r2 -= 1 |
| 78 | +# while l2 < r2 and nums[r2] == nums[r2 + 1]: |
| 79 | +# r2 -= 1 |
| 80 | +# if nums[l1] + nums[l2] + nums[r2] + nums[r1] < target: |
| 81 | +# l1 += 1 |
| 82 | +# while r1 - l1 > 2 and nums[l1] == nums[l1 - 1]: |
| 83 | +# l1 += 1 |
| 84 | +# else: |
| 85 | +# r1 -= 1 |
| 86 | +# while r1 - l1 > 2 and nums[r1] == nums[r1 + 1]: |
| 87 | +# r1 -= 1 |
| 88 | +# l2, r2 = l1 + 1, r1 - 1 |
| 89 | +# return out |
| 90 | + |
| 91 | + |
| 92 | +# for i in range(lens - 3): |
| 93 | +# if i and nums[i] == nums[i - 1]: |
| 94 | +# continue |
| 95 | +# for j in range(i + 1, lens - 2): |
| 96 | +# if j - i - 1 and nums[j] == nums[j - 1]: |
| 97 | +# continue |
| 98 | +# for k in range(j + 1, lens - 1): |
| 99 | +# if k - j - 1 and nums[k] == nums[k - 1]: |
| 100 | +# continue |
| 101 | +# for kk in range(k + 1, lens): |
| 102 | +# if kk - k - 1 and nums[kk] == nums[kk - 1]: |
| 103 | +# continue |
| 104 | +# if nums[i] + nums[j] + nums[k] + nums[kk] == target: |
| 105 | +# out.append([nums[i], nums[j], nums[k], nums[kk]]) |
| 106 | +# if nums[i] + nums[j] + nums[k] + nums[kk] > target: |
| 107 | +# break |
| 108 | +# if nums[k] > 0 and nums[i] + nums[j] + nums[k] > target: |
| 109 | +# break |
| 110 | +# if nums[j] > 0 and nums[i] + nums[j] > target: |
| 111 | +# break |
| 112 | +# if nums[i] > 0 and nums[i] > target: |
| 113 | +# break |
| 114 | +# return out |
| 115 | + |
| 116 | +fin = open("oo.xx", "r") |
| 117 | +fout = open("xx.oo", "w") |
| 118 | +n = int(fin.readline()[:-1]) |
| 119 | +nums = [int(x) for x in fin.readline()[:-1].split(',')] |
| 120 | +print(n, nums) |
| 121 | +out = Solution().fourSum(nums, n) |
| 122 | +for x in out: |
| 123 | + print(x) |
| 124 | + fout.write(str(x)) |
| 125 | + |
0 commit comments