码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode - Three Sum

时间:2015-12-07 14:07:07      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

题目:
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 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)

思路:
1)用递归,先排序,确定第一个,然后确定第二个,再寻找第三个。

package sum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ThreeSum {
    
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        int len;        
        if (nums == null || (len = nums.length) < 3) return res;
        Arrays.sort(nums);
        for (int i = 0; i < len - 2; ++i) {
            int rem = 0 - nums[i];
            List<Integer> subRes = new ArrayList<Integer>();
            subRes.add(nums[i]);
            GetTwo(rem, i + 1, nums, len, subRes, res);
            // Move forward if next element is the same as current element
            while (i < len - 1 && nums[i+1] == nums[i]) ++i;
        }
        
        return res;
    }
    
    private void GetTwo(int rem, int start, int[] nums, int len, List<Integer> subRes, List<List<Integer>> res) {
        for (int i = start; i < len - 1; ++i) {
            int last = rem - nums[i];
            List<Integer> cpySubRes = new ArrayList<Integer>(subRes);
            cpySubRes.add(nums[i]);
            GetLast(last, i + 1, nums, len, cpySubRes, res);
            // Move forward if next element is the same as current element
            while (i < len - 1 && nums[i+1] == nums[i]) ++i;
        }
    }
    
    private void GetLast(int rem, int start, int[] nums, int len, List<Integer> subRes, List<List<Integer>> res) {
        for (int i = start; i < len; ++i) {
            if (rem == nums[i]) {
                subRes.add(nums[i]);
                res.add(subRes);
                break;
            }
        }
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ThreeSum t = new ThreeSum();
        int[] S = { -1, 0, 1, 2, -1, -4 };
        List<List<Integer>> res = t.threeSum(S);
        for (List<Integer> subRes : res) {
            for (int i : subRes)
                System.out.print(i + "\t");
            System.out.println("\n");
        }
    }

}

2)非递归;排完序之后确立第一个元素,然后用两个指针指向剩下元素的头和尾,两边一夹,然后移动。

package sum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ThreeSum {
    
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        int len;        
        if (nums == null || (len = nums.length) < 3) return res;
        Arrays.sort(nums);
        for (int i = 0; i < len - 2;) {
            int rem = 0 - nums[i];            
            int left = i + 1;
            int right = len - 1;
            
            while (left < right) {
                if (nums[left] + nums[right] == rem) {
                    List<Integer> subRes = new ArrayList<Integer>();
                    subRes.add(nums[i]);
                    subRes.add(nums[left]);
                    subRes.add(nums[right]);
                    res.add(subRes);
                    
                    // Filter the duplicated elements.
                    do { ++left; } while (left < len && nums[left] == nums[left - 1]);
                    do { --right; } while (right >= 0 && nums[right + 1] == nums[right]);
                } else if (nums[left] + nums[right] < rem) {
                    left++;
                } else {
                    right--;
                }
            }
            
            // Move forward if the next element is the same as current element.
            do { ++i; } while (i < len && nums[i] == nums[i - 1]);
        }
        
        return res;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ThreeSum t = new ThreeSum();
        int[] S = { -2,0,1,1,2 };
        List<List<Integer>> res = t.threeSum(S);
        for (List<Integer> subRes : res) {
            for (int i : subRes)
                System.out.print(i + "\t");
            System.out.println("\n");
        }
    }

}

 

LeetCode - Three Sum

标签:

原文地址:http://www.cnblogs.com/shuaiwhu/p/5025767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!