|
| 1 | +# 15. 3Sum |
| 2 | + |
| 3 | +tags: Array, Two Pointers |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 题目原文 |
| 8 | +Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. |
| 9 | + |
| 10 | +Note: The solution set must not contain duplicate triplets. |
| 11 | + |
| 12 | +For example, given array S = [-1, 0, 1, 2, -1, -4], |
| 13 | + |
| 14 | +A solution set is: |
| 15 | + |
| 16 | + [ |
| 17 | + [-1, 0, 1], |
| 18 | + [-1, -1, 2] |
| 19 | + ] |
| 20 | + |
| 21 | +## 题目大意 |
| 22 | +给定一个整型数组num, 找出这个数组中满足这个条件的所有数字: num[i]+num[j]+num[k] = 0. 并且所有 |
| 23 | +的答案是要和其他不同的, 也就是说两个相同的答案是不被接受的。 |
| 24 | +题目的两点要求: |
| 25 | +1. 每个答案组里面的三个数字是要从大到小排列起来的。 |
| 26 | +2. 每个答案不可以和其他的答案相同。 |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +## 解题思路 |
| 31 | +### 网上分析1 |
| 32 | +题目分析: |
| 33 | + |
| 34 | +1. 每一个答案数组triplet中的元素是要求升序排列的。 |
| 35 | +2. 不能包含重复的答案数组。 |
| 36 | + |
| 37 | +解题思路: |
| 38 | +1. 根据第一点要求: 因为要求每个答案数组中的元素是升序排列的, 所以在开头我们要对数组进行排 |
| 39 | +序。 |
| 40 | +2. 根据第二点要求: 因为不能包含重复的答案数组, 所以我们要在代码里面做一切去掉重复的操作, 对 |
| 41 | +于数组, 这样的操作是相同的。 最开始我做leetcode的时候是把所有满足条件的答案数组存起来, 之后 |
| 42 | +再用map进行处理, 感觉那样太麻烦了, 所以这次给出的答案是不需要额外空间的。 |
| 43 | + |
| 44 | +时间复杂度分析: |
| 45 | +对于这道题, 因为是要找三个元素, 所以怎样都要O(n2)的时间复杂度, 目前我没有想出来O(n)时间复杂度 |
| 46 | +的解法。 |
| 47 | + |
| 48 | +归根结底, 其实这是two pointers的想法, 定位其中两个指针, 根据和的大小来移动另外一个。 解题中要 |
| 49 | +注意的就是一些细节问题。 好了, 上代码吧。 |
| 50 | + |
| 51 | +### 网上分析2 |
| 52 | +the key idea is the same as the TwoSum problem. When we fix the 1st number,the 2nd and 3rd number can be found following the same reasoning as TwoSum. |
| 53 | + |
| 54 | +The only difference is that, the TwoSum problem of LEETCODE has a unique solution.However, in ThreeSum, we have multiple duplicate solutions that can be found. Most ofthe OLE errors happened here because you could've ended up with a solution with so manyduplicates. |
| 55 | + |
| 56 | +The naive solution for the duplicates will be using the STL methods like below : |
| 57 | + |
| 58 | +std::sort(res.begin(), res.end()); |
| 59 | + |
| 60 | +res.erase(unique(res.begin(), res.end()), res.end()); |
| 61 | + |
| 62 | +But according to my submissions, this way will cause you double your time consumingalmostly. |
| 63 | +A better approach is that, to jump over the number which has been scanned, no matter itis part of some solution or not. |
| 64 | + |
| 65 | +If the three numbers formed a solution, we can safely ignore all the duplicates of them. |
| 66 | +We can do this to all the three numbers such that we can remove the duplicates. |
| 67 | +Here's my AC C++ Code: |
| 68 | + |
| 69 | +### 我的分析: |
| 70 | + |
| 71 | +主要是先将原数组排序,并充分利用排序好的数组的特性。去除重复答案的方法是,先按index从小到大确定第一个元素,在按(bengin=index)++和(end)--的顺序确定第二个元素和第三个元素,当begin=begin+1是跳过,end同理,第一个元素的所有可能循环完成后,index++之前也要判断index是否等于index+1。总而言之就是利用排序号数组特性保证每个元素都各不相同。先确定答案中最小的数,防止答案中出现重复的组合 |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +## 代码 |
| 76 | +### [c++代码](./src/cpp/3Sum.cpp) |
| 77 | +```c++ |
| 78 | +class Solution { |
| 79 | +public: |
| 80 | + vector<vector<int>> threeSum(vector<int>& nums) { |
| 81 | + std::sort(nums.begin(),nums.end()); |
| 82 | + vector<vector<int >> res; |
| 83 | + for(int i=0;i<nums.size();i++){ |
| 84 | + int target=-nums[i]; |
| 85 | + int front=i+1; |
| 86 | + int back=nums.size()-1; |
| 87 | + while(front<back){ |
| 88 | + int sum=nums[front]+nums[back]; |
| 89 | + if(sum>target) |
| 90 | + back--; |
| 91 | + else if(sum<target) |
| 92 | + front++; |
| 93 | + else{ |
| 94 | + vector<int> tri(3,0); |
| 95 | + tri[0]=nums[i]; |
| 96 | + tri[1]=nums[front]; |
| 97 | + tri[2]=nums[back]; |
| 98 | + res.push_back(tri); |
| 99 | + |
| 100 | + while(front<back&&nums[front]==tri[1]) front++; |
| 101 | + while(front<back&&nums[back]==tri[2]) back--; |
| 102 | + |
| 103 | + |
| 104 | + } |
| 105 | + while(i+1<nums.size()&&nums[i+1]==nums[i]) i++; |
| 106 | + } |
| 107 | + } |
| 108 | + return res; |
| 109 | + } |
| 110 | +}; |
| 111 | +``` |
| 112 | + |
| 113 | +### [python代码](./src/python/3Sum.py) |
| 114 | +```python |
| 115 | +cimport collections |
| 116 | + |
| 117 | + |
| 118 | +class Solution(object): |
| 119 | + def threeSum(self, nums): |
| 120 | + """ |
| 121 | +:type nums: List[int] |
| 122 | +:rtype: List[List[int]] |
| 123 | +""" |
| 124 | + nums, result, i = sorted(nums), [], 0 |
| 125 | + while i < len(nums) - 2: |
| 126 | + if i == 0 or nums[i] != nums[i - 1]: |
| 127 | + j, k = i + 1, len(nums) - 1 |
| 128 | + while j < k: |
| 129 | + if nums[i] + nums[j] + nums[k] < 0: |
| 130 | + j += 1 |
| 131 | + elif nums[i] + nums[j] + nums[k] > 0: |
| 132 | + k -= 1 |
| 133 | + else: |
| 134 | + result.append([nums[i], nums[j], nums[k]]) |
| 135 | + j, k = j + 1, k - 1 |
| 136 | + while j < k and nums[j] == nums[j - 1]: |
| 137 | + j += 1 |
| 138 | + while j < k and nums[k] == nums[k + 1]: |
| 139 | + k -= 1 |
| 140 | + i += 1 |
| 141 | + return result |
| 142 | + |
| 143 | + def threeSum2(self, nums): |
| 144 | + """ |
| 145 | +:type nums: List[int] |
| 146 | +:rtype: List[List[int]] |
| 147 | +""" |
| 148 | + d = collections.Counter(nums) |
| 149 | + nums_2 = [x[0] for x in d.items() if x[1] > 1] |
| 150 | + nums_new = sorted([x[0] for x in d.items()]) |
| 151 | + rtn = [[0, 0, 0]] if d[0] >= 3 else [] |
| 152 | + for i, j in enumerate(nums_new): |
| 153 | + if j <= 0: |
| 154 | + numss2 = nums_new[i + 1:] |
| 155 | + for x, y in enumerate(numss2): |
| 156 | + if 0 - j - y in [j, y] and 0 - j - y in nums_2: |
| 157 | + if sorted([j, y, 0 - j - y]) not in rtn: |
| 158 | + rtn.append(sorted([j, y, 0 - j - y])) |
| 159 | + if 0 - j - y not in [j, y] and 0 - j - y in nums_new: |
| 160 | + if sorted([j, y, 0 - j - y]) not in rtn: |
| 161 | + rtn.append(sorted([j, y, 0 - j - y])) |
| 162 | + return rtn |
| 163 | + |
| 164 | +if __name__ == '__main__': |
| 165 | + result = Solution().threeSum([-1, 0, 1, 2, -1, -4]) |
| 166 | + print result |
| 167 | + |
| 168 | +``` |
0 commit comments