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: 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]
]
分析:拿到题目,暴力for循环,然后就超时了。这是超时的代码。
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        if(nums!=null) {
        int max = 0, min = 0;
        int index1, index2, index3;
        index1 = index2 = index3 = 0;
        for (; index1 < nums.length; index1++) {
            for (index2 = index1 + 1; index2 < nums.length; index2++) {
                for (index3 = index2 + 1; index3 < nums.length; index3++) {
                    // 如果满足条件合为1
                    if (nums[index1] + nums[index2] + nums[index3] == 0) {
                        // 计算三个数中最大值最小值
                        min = Math.min(Math.min(nums[index1], nums[index2]), nums[index3]);
                        max = Math.max(Math.max(nums[index1], nums[index2]), nums[index3]);
                        if (list == null) {
                            list = new ArrayList<>();
                        }
                        list.add(Arrays.asList(min, 0 - min - max, max));

                    }
                }
            }
        }
        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                if (list.get(i).get(0) == list.get(j).get(0) && list.get(i).get(2) == list.get(j).get(2)) {
                    list.get(j).set(0, 1);
                    list.get(j).set(1, 1);
                    list.get(j).set(2, 1);
                }
            }
        }
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).get(0) == 1 && list.get(i).get(1) == 1 && list.get(i).get(2) == 1) {
                    list.remove(i);
                    i--;
                    continue;
                }
            }
        return list;
        }else
        return list;
    }
}

没办法,看看别人的答案。

别人的代码比较简单,想法也很简单,只是优化很好,先排序,然后从头开始选定一个数,再从这个数的后一位和数组最后一位开始向前查找,如果满足和为0的,即添加进list,这里优化的点是,如果一开始选定的这个数在向后移动的过程中,他与前一个数一样,那么就跳过这一次,因为会重复,如果后面两个数在移动过程中也遇到了连续相同的两个数,也跳过这次,保证不重复。

public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        
        for (int i = 0; i < nums.length - 2; i++) {
            if (i > 0 && nums[i] == nums[i - 1])
                continue;
            int target = -nums[i];
            int lo = i + 1;
            int hi = nums.length - 1;
            while (lo < hi) {
                if (nums[lo] + nums[hi] == target) {
                    list.add(Arrays.asList(nums[i], nums[lo], nums[hi]));
                    lo++;
                    hi--;
                    while (lo<hi&&nums[lo] == nums[lo-1])
                        lo++;
                    while (lo<hi&&nums[hi] == nums[hi+1])
                        hi--;
                } else if (nums[lo] + nums[hi] > target) {
                    hi--;
                } else {
                    lo++;
                }
            }
        }
        return list;
    }

还有一个比较奇葩的错误,中间声明的三个变量target/lo/hi,如果我拿到for语句外面声明,在里面赋值,就超时了,拿进来声明并赋值就可以通过,估计是声明和赋值分开操作会比较耗时吧。

posted @ 2018-03-07 18:34  真的是从入门开始  阅读(103)  评论(0编辑  收藏  举报