LeetCode_15---3Sum

来源:互联网 发布:mysql work导出数据库 编辑:程序博客网 时间:2024/05/22 11:31

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 Exceededpublic 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 Exceededpublic 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++;elsek--;}}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++;elsehi--;}}}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
    原创粉丝点击