LeetCode 3sum 问题

1、

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)

2、

这道题引申与2sum问题,之后还可以继续引申4sum。

具体的思路就是双指针的灵活应用;

3、我的解法:

 1 List<List<Integer>> ret = new ArrayList<List<Integer>>();
 2     public List<List<Integer>> threeSum(int[] num) {
 3
 4         if (num == null || num.length < 3) return ret;
 5
 6         Arrays.sort(num);
 7
 8         int len = num.length;
 9         for (int i = 0; i < len-2; i++) {
10             if (i > 0 && num[i] == num[i-1]) continue;
11             find(num, i+1, len-1, num[i]); //寻找两个数与num[i]的和为0
12         }
13
14         return ret;
15     }
16     public void find(int[] num, int begin, int end, int target) {
17         int l = begin, r = end;
18         while (l < r) {
19             if (num[l] + num[r] + target == 0) {
20                 List<Integer> ans = new ArrayList<Integer>();
21                 ans.add(target);
22                 ans.add(num[l]);
23                 ans.add(num[r]);
24                 ret.add(ans); //放入结果集中
25                 while (l < r && num[l] == num[l+1]) l++;
26                 while (l < r && num[r] == num[r-1]) r--;
27                 l++;
28                 r--;
29             } else if (num[l] + num[r] + target < 0) {
30                 l++;
31             } else {
32                 r--;
33             }
34         }
35     }  
时间: 2024-10-29 19:07:36

LeetCode 3sum 问题的相关文章

[leetcode]3Sum Closest @ Python

原题地址:http://oj.leetcode.com/problems/3sum-closest/ 题意:数组中每三个元素进行求和,找出所有和中大小最接近target的和,并返回这个和与target之间的差值. 解题思路:使用一个变量mindiff来监测和与target之间的差值,如果差值为0,直接返回sum值. 代码: class Solution: # @return an integer def threeSumClosest(self, num, target): num.sort()

leetcode 3Sum 3Sum Closest 4Sum

这几个题很典型也是国外一些知名公司经常会问到的题 3Sum: 排序,避免重复,时间复杂度O(n^2) class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { int len=num.size(); sort(num.begin(),num.begin()+len); vector<vector<int> > ret; ret.clear(); i

leetcode:3sum closet

题目:给一个数组和给定的目标值,要求在数组里找出三个元素,这三个元素的和最接近目标值,当然等于是最好的. 用3sum的方法,把判定条件作些修改. int twoSum(vector<int> &num, int start, int target) { if(num.size() < 3 || start >= num.size()) return -target; int head = start; int tail = num.size() - 1; int resul

[LeetCode]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 so

Leetcode——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] 3Sum Closest

This problem is very similar to 3Sum. You only need to maintain a variable for the sum that is closet to target. Also, some corner cases need to be handled; for example, nums does not have more than 2 elements. The code is as follows, which is quite

[LeetCode] 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 = {-1 2

LeetCode——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 = {-1 2

LeetCode——3Sum &amp; 3Sum Closest

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)

[LeetCode] 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 solut