Java [leetcode 15] 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, abc)
  • 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)

解题思路

对于这样的无序数组首先进行升序排列,使之变成非递减数组,调用java自带的Arrays.sort()方法即可。

然后每次固定最小的那个数,在后面的数组中找出另外两个数之和为该数的相反数即可。

具体的过程可参考博客:http://blog.csdn.net/zhouworld16/article/details/16917071

代码实现:

public class Solution {
    List<List<Integer>> ans = new ArrayList<List<Integer>>();

    public List<List<Integer>> threeSum(int[] nums) {
        int length = nums.length;
        if (nums == null || length < 3)
            return ans;
        Arrays.sort(nums);
        for (int i = 0; i < length - 2; ++i) {
            if (i > 0 && nums[i] == nums[i - 1])
                continue;
            findTwoSum(nums, i + 1, length - 1, nums[i]);
        }
        return ans;
    }

    public void findTwoSum(int[] num, int begin, int end, int target) {
        while (begin < end) {
            if (num[begin] + num[end] + target == 0) {
                List<Integer> list = new ArrayList<Integer>();
                list.add(target);
                list.add(num[begin]);
                list.add(num[end]);
                ans.add(list);
                while (begin < end && num[begin + 1] == num[begin])
                    begin++;
                begin++;
                while (begin < end && num[end - 1] == num[end])
                    end--;
                end--;
            } else if (num[begin] + num[end] + target > 0)
                end--;
            else
                begin++;
        }
    }
}
时间: 2024-10-08 10:29:13

Java [leetcode 15] 3Sum的相关文章

[LeetCode][15]3Sum解析与快速排序算法-Java实现

Q: 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: The solution set must not contain duplicate triplets. For example, given array S = [-

LeetCode 15 3Sum (C,C++,Java,Python)

Problem: 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] 15. 3Sum Java

题目: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: The solution set must not contain duplicate triplets. For example, given array S = [-

LeetCode 15. 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: The solution set must not contain duplicate triplets. For example, given array S = [-1,

Java [leetcode 16] 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] 15. 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: The solution set must not contain duplicate triplets. For example, given array S = [-1,

LeetCode #15 3Sum (M)

[Problem] 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 15. 3Sum 双指针

题目链接 给n个数, 找出三个数相加结果为0的所有的组, 不可重复. 用双指针的思想,O(n^2)暴力的找, 注意判重复. 1 class Solution { 2 public: 3 vector<vector<int>> threeSum(vector<int>& nums) { 4 int sz = nums.size(); 5 vector <vector<int> > ans; 6 vector <int> tmp;

leetCode 15. 3Sum (3个数) 解题思路和方法

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