LeetCode:18. 4Sum(Medium)

 1. 原题链接

https://leetcode.com/problems/4sum/description/

2. 题目要求

给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个数之和等于目标整数target。请找出所有满足此条件的四个整数。

3. 解题思路

先对nums进行排序,然后采用两层for循环来确定前两个数字,最后在第二层for循环中确定后两个数字。

注意可能存在重复解!!

 如下图所示,对Input先进行排序:[-4, -1, -1,0, 1,2],target = -1

存在两个“-1”,因此要考虑结果去重。

使用 if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) 来对第一层for循环,即第一个数字去重。

如果对第二层for循环采用同样的方法去重,很可能导致丢失一个解,返回下图的错误结果。

[-4, -1, -1,0, 1,2],绿色表示第一层for循环遍历到的位置,红色表示第二层for循环开始的位置。如果使用 if (nums[j] != nums[j-1]) 来去重,就会跳过“-1”。

因此引入一个计数器count,来判断第二层for循环执行的次数。当count==1,即第二层for循环刚开始一次时,避免“-1”和“-1”的重复误判。

 4. 代码实现

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

public class FourSum18 {

    public static void main(String[] args) {
        int[] nums = {-1,-3,-2,2,3,-3,0,-4};
        int target = 4;
        List<List<Integer>> res = FourSum18.fourSum(nums, target);
        for (List list : res) {
            System.out.println(list);
        }
    }

    public static List<List<Integer>> fourSum(int[] nums, int target) {
        ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
        Arrays.sort(nums);  // 对nums进行排序

        for (int i = 0; i < nums.length - 3; i++) {
            int sum1 = target - nums[i];
            if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {  // 去除遍历第一个数字时重复的结果
                int count =0;
                for (int j = i + 1; j < nums.length - 2; j++) {
                    /**
                     * 需要判断遍历第二个数字存在重复解的可能
                     * 要同时考虑第一次遍历的位置
                     * 用count计数第二个数遍历的次数
                     */
                    count++;

                    if (nums[j] != nums[j-1]|| count==1) {  // 去除遍历第二个数字时重复的结果
                        int sum2 = sum1 - nums[j], l = j + 1, r = nums.length - 1;
                        while (l < r) {
                            if (nums[l] + nums[r] == sum2) {
                                res.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
                                while (l < r && nums[l] == nums[l + 1]) l++;
                                while (l < r && nums[r] == nums[r - 1]) r--;
                                l++;
                                r--;
                            } else if (sum2 < nums[l] + nums[r]) {
                                while (l < r && nums[r] == nums[r - 1]) r--;
                                r--;

                            } else {
                                while (l < r && nums[l] == nums[l + 1]) l++;
                                l++;
                            }
                        }
                    }
                }

            }
        }
        return res;
    }
}

  

posted @ 2017-12-23 11:22  一只敲码的猫  阅读(227)  评论(0编辑  收藏  举报