|
| 1 | +''' |
| 2 | +
|
| 3 | +
|
| 4 | +Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. |
| 5 | +
|
| 6 | +Note: |
| 7 | +
|
| 8 | +The solution set must not contain duplicate triplets. |
| 9 | +
|
| 10 | +Example: |
| 11 | +
|
| 12 | +Given array nums = [-1, 0, 1, 2, -1, -4], |
| 13 | +
|
| 14 | +A solution set is: |
| 15 | +[ |
| 16 | + [-1, 0, 1], |
| 17 | + [-1, -1, 2] |
| 18 | +] |
| 19 | +
|
| 20 | +
|
| 21 | +
|
| 22 | +''' |
| 23 | + |
| 24 | +class Solution: |
| 25 | + def threeSum(self, nums: 'List[int]') -> 'List[List[int]]': |
| 26 | + |
| 27 | + # Approach one |
| 28 | +# from collections import Counter |
| 29 | +# dic = Counter(nums) |
| 30 | +# res = [] |
| 31 | +# new_nums = [] |
| 32 | +# for k,v in dic.items(): |
| 33 | +# if k == 0 and v > 2: |
| 34 | +# res.append([0,0,0]) |
| 35 | +# new_nums.append(0) |
| 36 | +# continue |
| 37 | +# if v >= 2: |
| 38 | +# if k != 0 and -2*k in dic.keys(): |
| 39 | +# res.append([k,k,-2*k]) |
| 40 | +# new_nums.append(k) |
| 41 | +# else: |
| 42 | +# new_nums.append(k) |
| 43 | + |
| 44 | +# nums = sorted(new_nums) |
| 45 | +# length = len(nums) - 1 |
| 46 | +# for i,n in enumerate(nums): |
| 47 | +# if n > 0 : break |
| 48 | +# l, r = i + 1, length |
| 49 | +# while l < r: |
| 50 | +# result = nums[l] + nums[r] + n |
| 51 | +# if result > 0: |
| 52 | +# r -= 1 |
| 53 | +# elif result == 0: |
| 54 | +# res.append([n, nums[l], nums[r]]) |
| 55 | +# l += 1 |
| 56 | +# r -= 1 |
| 57 | +# else: |
| 58 | +# l += 1 |
| 59 | +# return res |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + # Approach two 排序后撞指针 O(n2) |
| 67 | + # nums = sorted(nums) |
| 68 | + # res = [] |
| 69 | + # length = len(nums) - 1 |
| 70 | + # for i,n in enumerate(nums[:-2]): |
| 71 | + # if i > 0 and n == nums[i-1]: continue |
| 72 | + # if n > 0 : break |
| 73 | + # l, r = i + 1, length |
| 74 | + # while l < r: |
| 75 | + # result = nums[l] + nums[r] + n |
| 76 | + # if result > 0: |
| 77 | + # r -= 1 |
| 78 | + # elif result == 0: |
| 79 | + # res.append([n, nums[l], nums[r]]) |
| 80 | + # l += 1 |
| 81 | + # while l < r and nums[l - 1] == nums[l]: |
| 82 | + # l += 1 |
| 83 | + # r -= 1 |
| 84 | + # while l < r and nums[r] == nums[r + 1]: |
| 85 | + # r -= 1 |
| 86 | + # else: |
| 87 | + # l += 1 |
| 88 | + # return res |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + # Approach three O(j*k) faster than 98% ,但消耗的内存增加 |
| 95 | + from collections import Counter |
| 96 | + dic = Counter(nums) |
| 97 | + pos = [x for x in dic if x > 0] |
| 98 | + neg = [x for x in dic if x < 0] |
| 99 | + res = [] |
| 100 | + |
| 101 | + if dic.get(0, 0) > 2: res.append([0, 0, 0]) # 单独处理特殊的零 |
| 102 | + for x in pos: # a、b、c 三数加和为零,若 a < 0 , b > 0 , 则 a < -c < b |
| 103 | + for y in neg: |
| 104 | + s = -(x + y) |
| 105 | + if s in dic: |
| 106 | + if s == x and dic[x] > 1: |
| 107 | + res.append([x, x, y]) |
| 108 | + elif s == y and dic[y] > 1: |
| 109 | + res.append([x, y, y]) |
| 110 | + elif y < s < x: |
| 111 | + res.append([x, y, s]) |
| 112 | + return res |
0 commit comments