4sum implementation in Java from leetcode

前端 未结 6 755
生来不讨喜
生来不讨喜 2021-01-07 11:50

The problem statement from leetcode says:

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Fin

6条回答
  •  礼貌的吻别
    2021-01-07 11:58

    public ArrayList> fourSum(int[] num, int target) {
        Arrays.sort(num);
        ArrayList> res=new ArrayList>();
        int i=0;
        while(i t=new ArrayList();
                        t.add(num[i]);
                        t.add(num[j]);
                        t.add(num[left]);
                        t.add(num[right]);
                        res.add(t);
                        left++;
                        right--;
                        while(lefttarget-num[i]-num[j])
                        right--;
                    else
                        left++;
                }
                j++;
                while(j

提交回复
热议问题