LeetCode-array-4Sum

题目:
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

和求三个和的题目差不多,多了个target值

思路和3Sum一样,这里用到double for循环,3Sum的时候是固定循环第i个数,让(i+1)和(数组.length-1)两指针向中间移动,
在4Sum中我这里也是用这样的思路,代码如下

import java.net.Inet4Address;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Wangjianxin on 2017/7/5 0005.
 */
public class FourSum {

    public static void main(String[] args) {
        int [] s = {-1,0,-5,-2,-2,-4,0,1,-2};
        int t = -9;
        System.out.println(fourSum(s,t));
    }
    public static List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> lists = new LinkedList<List<Integer>>();
        int len = nums.length;

        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
        for(int i = 0; i<len ;i ++){
            int start = i;

            //排重
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            if(nums.length < 4){
                break;
            }
            for(int j = i+1;j<len ;j++){

                int left1 = j;
                int left2 = j+1;
                int right = len-1;

                if(nums[0] >0){
                    break;
                }
                //排重
                if(j > i+1 && nums[j-1] == nums[j]){
                    continue;
                }

                while ( left1 < left2 && left2 < right){

                    int sum = nums[start] + nums[left1] + nums[left2] + nums[right];
                    if(sum > target){
                        right--;
                    }
                    if(sum < target){
                        left2 ++;
                    }
                    //排重 俩指针
                    if(left2 > j+1 && nums[left2] == nums[left2 - 1]){
                        left2 ++;
                    }
                    if(right < len - 1 && nums[right] == nums[right + 1]){
                        right --;
                    }
                    else if(sum == target){
                        List<Integer> list = new LinkedList<Integer>();
                        list.add(nums[start]);
                        list.add(nums[left1]);
                        list.add(nums[left2]);
                        list.add(nums[right]);
                        lists.add(list);
                        left2++;
                        right--;
                    }
                }
            }
        }
        return lists;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,354评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,487评论 0 23
  • 杜尚晚年时曾被问起:应该怎样去做一个艺术家?他回答:“悄没声儿地做你自己的东西。”他一辈子做的东西都是在这样的状态...
    蒋菱阅读 1,313评论 0 0
  • 关于慕寒的一百条 1.远山长慕,寒鸦声渡 2.慕寒这个名字是他做梦梦到哒 3.出道十年了,是个老艺人~~ 4.声音...
    小南乔阅读 5,326评论 4 14
  • 昔日。天真如我 如今。现实如你
    扑克小王阅读 1,904评论 0 0