[LeetCode]#13 3sum

一、题目

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)

二、思路我的思路一直很简单,当初在做2Sum的时候,就是把求和问题变成了一个查找问题,定下一个数a,去找target-a在不在剩余的数里。3Sum也这么做,但是效率明显低多了。而且这题让输出所有可能的答案组合,牵扯到重复的问题。做法还是按照变求和为查询,定下a,b,找target-(a+b),唯一做的优化就是用哈希表存下已有的(a,b),这样不至于做重复的查找。但是这样毕竟不是办法,以后做4sum就要4层循环了,不是个事。看别人的答案,是从两边往中间找,这个挺好的。准备在3Sum Closet里试一下这种方法。

三、代码
 1 class Solution:
 2     # @param {integer[]} nums
 3     # @return {integer[][]}
 4     def threeSum(self, nums):
 5         nums.sort()
 6         passed_list = []
 7         result = []
 8         record = {}
 9         result_dict = {}
10         if len(nums) < 3:
11             return []
12         else:
13             for i in range(len(nums)):
14                 for j in range(i + 1, len(nums)):
15                     a = nums[i]
16                     b = nums[j]
17                     if (a, b) in record:
18                         pass
19                     else:
20                         record[(a, b)] = 0
21                         target = 0 - a - b
22                         if target in nums[j + 1:]:
23                             passed_list.append(a)
24                             passed_list.append(b)
25                             passed_list.append(target)
26                             if str(passed_list) in result_dict:
27                                 passed_list = []
28                                 pass
29                             else:
30                                 result.append(passed_list)
31                                 result_dict[str(passed_list)] = 0
32                                 passed_list = []
33             return result

四、总结

准备把n-sum这类问题全部做完,然后总结一下这类题的规律。

时间: 2024-12-15 15:21:01

[LeetCode]#13 3sum的相关文章

[Leetcode][016] 3Sum Closest (Java)

题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum 1 public class Solution { 2 public int threeSumClosest(int[] nums, int target) { 3 int result = target; 4 int

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

LeetCode 015 3Sum

[题目] 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. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The

LeetCode 016 3Sum Closest

[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {

[LeetCode] 015. 3Sum (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 015.3Sum (Medium) 链接: 题目:https://oj.leetcode.com/problems/3sum/ 代码(github):https://github.com/illuz/leetcode 题意: 在给定数列中找出三个数,使和为 0. 分析: 先排序,再左右夹逼,复杂度 O(n*n).

[C++]LeetCode: 70 3Sum

题目: 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. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s

[LeetCode][JavaScript]3Sum Closest

3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given arr

[Leetcode][015] 3Sum (Java)

题目在这里: https://leetcode.com/problems/3sum/ [标签] Array; Two Pointers [个人分析] 老实交待,这个题卡半天,第一次做不会,抄别人的.过了很久,第二次做,还是不会…….好几次都是Time Limited Error.在看过正确答案之后,才知道是用的Two Pointers + sort 做的优化. 怎么优化? 简单说,就是通过 排序 + 跳过重复(利用Two Pointers) 来达到题目中避免 duplicates的要求. 核心思

LeetCode 259. 3Sum Smaller (三数之和较小值) $

Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target. For example, given nums = [-2, 0, 1, 3], and target = 2. Retu