LeetCode_15---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)

翻译:

    Code:




    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author MohnSnow
     * @time 2015年6月4日 上午9:45:37
     * 
     */
    public class LeetCode15 {
    
    	/**
    	 * @param argsmengdx
    	 *            -fnst
    	 */
    	//brute---O(n3)--- Time Limit Exceeded
    	public static List<List<Integer>> threeSum(int[] nums) {
    		int len = nums.length;
    		List<List<Integer>> list = new ArrayList<List<Integer>>();
    		for (int i = 0; i < len - 2; i++) {
    			for (int j = i + 1; j < len - 1; j++) {
    				for (int m = j + 1; m < len; m++) {
    					if (nums[i] + nums[j] + nums[m] == 0) {
    						List<Integer> aList = new ArrayList<Integer>();
    						aList.add(nums[i]);
    						aList.add(nums[j]);
    						aList.add(nums[m]);
    						list.add(aList);
    					}
    				}
    			}
    		}
    		return list;
    	}
    
    	// Time Limit Exceeded---O(n2)---忘记限制条件:相同结果只能出现一次
    	public static List<List<Integer>> threeSum1(int[] nums) {
    		int len = nums.length;
    		List<List<Integer>> list = new ArrayList<List<Integer>>();
    		Arrays.sort(nums);
    		System.out.println(Arrays.toString(nums));
    		for (int i = 0; i < len - 2 && nums[i] <= 0; i++) {
    			for (int j = i + 1, m = len - 1; j < m;) {
    				if (nums[i] + nums[j] + nums[m] == 0) {
    					List<Integer> aList = new ArrayList<Integer>();
    					aList.add(nums[i]);
    					aList.add(nums[j]);
    					aList.add(nums[m]);
    					list.add(aList);
    					j++;
    					m--;
    				} else if (nums[i] + nums[j] + nums[m] < 0) {
    					j++;
    				} else {
    					m--;
    				}
    			}
    		}
    		return list;
    	}
    
    	//https://leetcode.com/discuss/31565/my-accepted-java-solution-using-hashset-the-time-complexity
    	//Time Limit Exceeded
    	public static List<List<Integer>> threeSum2(int[] num) {
    		Arrays.sort(num);
    		List<List<Integer>> list = new ArrayList<List<Integer>>();
    		HashSet<List<Integer>> set = new HashSet<List<Integer>>();
    		for (int i = 0; i < num.length - 2; i++)
    		{
    			for (int j = i + 1, k = num.length - 1; j < k;)
    			{
    				if (num[i] + num[j] + num[k] == 0)
    				{
    					List<Integer> l = new ArrayList<Integer>();
    					l.add(num[i]);
    					l.add(num[j]);
    					l.add(num[k]);
    					if (set.add(l))
    						list.add(l);
    					j++;
    					k--;
    				}
    				else if (num[i] + num[j] + num[k] < 0)
    					j++;
    				else
    					k--;
    			}
    		}
    		return list;
    	}
    //相同方法,C++版本68msA,Java版本去总是提示超时
    	public static List<List<Integer>> threeSum3(int[] num) {
    		Arrays.sort(num);
    		List<List<Integer>> res = new LinkedList<>();
    		for (int i = 0; i < num.length - 2; i++) {
    			if (i == 0 || (i > 0 && num[i] != num[i - 1])) {//注意这个条件
    				int lo = i + 1, hi = num.length - 1, sum = 0 - num[i];
    				while (lo < hi) {
    					if (num[lo] + num[hi] == sum) {
    						res.add(Arrays.asList(num[i], num[lo], num[hi]));
    						while (lo < hi && num[lo] == num[lo + 1])
    							lo++;
    						while (lo < hi && num[hi] == num[hi - 1])
    							hi--;
    						lo++;
    						hi--;
    					} else if (num[lo] + num[hi] < sum)
    						lo++;
    					else
    						hi--;
    				}
    			}
    		}
    		return res;
    	}
    
    	public static void main(String[] args) {
    		int[] strs = { 1, -1, 0, 7, -7, -7 };
    		int[] strs1 = { -9, 14, -7, -8, 9, 1, -10, -8, 13, 12, 6, 9, 3, -3, -15, -15, 1, 8, -7, -4, -6, 8, 2, -10, 8, 11, -15, 3, 0, -11, -1, -1, 10, 0, 6, 5, -14, 3, 12, -15, -7, -5, 9, 11, -1, 1, 3, -15, -5, 11, -12, -4, -4, -2, -6, -10, -6, -6, 0, 2, -9, 14, -14, -14, -9, -1, -2, -7, -12, -13, -15, -4, -3, 1, 14, 3, -12, 3, 3, -10, -9, -1, -7, 3, 12, -6, 0, 13, 4, -15, 0, 2, 6, 1, 3, 13, 8, -13, 13, 11, 11, 13, 14, -6 };
    		System.out.println(Arrays.toString(strs));
    		System.out.println(threeSum(strs));
    		System.out.println(threeSum1(strs1));
    		System.out.println(threeSum2(strs1));
    		System.out.println(threeSum3(strs1));
    		//System.out.println(threeSum2(strs));
    	}
    
    }
    


    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值