|
| 1 | +from typing import List |
| 2 | +import functools as ft |
| 3 | +from functools import partial as par |
| 4 | +def F(*z): |
| 5 | + z = [*z] |
| 6 | + z[0] = [z[0]] |
| 7 | + return [*ft.reduce(lambda x, y: map(y, x), z)][0] |
| 8 | +FF = lambda *z: [*ft.reduce(lambda x, y: map(y, x), z)] |
| 9 | +fyx = lambda f, *x: lambda *y: f(*y, *x) |
| 10 | + |
| 11 | + |
| 12 | +class Solution: |
| 13 | + def quicksort(l, r, obj): |
| 14 | + if not l < r: |
| 15 | + return obj |
| 16 | + i, j, x = l, r, obj[r] |
| 17 | + while (i < j): |
| 18 | + while (i < j and obj[i] <= x): |
| 19 | + i += 1 |
| 20 | + obj[j] = obj[i] |
| 21 | + while (i < j and obj[j] > x): |
| 22 | + j -= 1 |
| 23 | + obj[i] = obj[j] |
| 24 | + obj[i] = x |
| 25 | + Solution.quicksort(l, i - 1, obj) |
| 26 | + Solution.quicksort(i + 1, r, obj) |
| 27 | + return obj |
| 28 | + def threeSum(self, nums: List[int]) -> List[List[int]]: |
| 29 | + lenNums = len(nums) |
| 30 | + nums = Solution.quicksort(0, lenNums - 1, nums) |
| 31 | + record, out = {}, [] |
| 32 | + for x in nums: |
| 33 | + if x in record: |
| 34 | + record[x] += 1 |
| 35 | + else: |
| 36 | + record[x] = 1 |
| 37 | + for i in range(lenNums): |
| 38 | + if nums[i] > 0: |
| 39 | + break |
| 40 | + if i and nums[i] == nums[i - 1]: |
| 41 | + continue |
| 42 | + record[nums[i]] -= 1 |
| 43 | + for j in range(i + 1, lenNums): |
| 44 | + if nums[i] + nums[j] > 0: |
| 45 | + break |
| 46 | + if j - i - 1 and nums[j] == nums[j - 1]: |
| 47 | + continue |
| 48 | + record[nums[j]] -= 1 |
| 49 | + test = - nums[i] - nums[j] |
| 50 | + if (test in record and test >= nums[j] |
| 51 | + and record[test]): |
| 52 | + out.append([nums[i], nums[j], test]) |
| 53 | + record[nums[j]] += 1 |
| 54 | + record[nums[i]] += 1 |
| 55 | + return out |
| 56 | + |
| 57 | +fin = open("oo.xx", "r") |
| 58 | +fout = open("xx.oo", "w") |
| 59 | +a = [int(x) for x in fin.readline()[:-1].split(' ')] |
| 60 | +out = Solution().threeSum(a) |
| 61 | +print(out) |
| 62 | +fout.write(str(out)) |
| 63 | + |
0 commit comments