首页 > 其他分享 >18. 四数之和

18. 四数之和

时间:2022-11-10 11:00:34浏览次数:57  
标签:四数 target nums int 18 right && left

18. 四数之和

给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):

  • 0 <= a, b, c, d < n
  • abcd 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意顺序 返回答案 。

示例 1:

输入:nums = [1,0,-1,0,-2,2], target = 0
输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

示例 2:

输入:nums = [2,2,2,2,2], target = 8
输出:[[2,2,2,2]]

提示:

  • 1 <= nums.length <= 200
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0 && nums[i] > target) return res;// 剪枝

            if (i > 0 && nums[i] == nums[i-1]) continue;// 去重

            for (int j = i + 1; j < nums.length; j++) {
                
                if (j > i + 1 && nums[j - 1] == nums[j]) continue;
                int left = j + 1;
                int right = nums.length - 1;
                
                while (right > left) {
                    long sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum > target) {
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));

                        while (right > left && nums[right] == nums[right-1]) right--;
                        while (right > left && nums[left] == nums[left+1]) left++;
                        right--;
                        left++;
                    }
                }
            }
        }
        return res;
    }
}

 

标签:四数,target,nums,int,18,right,&&,left
From: https://www.cnblogs.com/fulaien/p/16876379.html

相关文章

  • 问题 E: 零基础学C/C++184——吉祥数
    可以利用cishu数组来记录每个数字是否被淘汰。本题的关键就是再算出如题的数组b的时候向a数组检查是,不能因为第一个数被淘汰而不算他的吉祥数,应在一轮计算结束的时候遍历......
  • HDU 3518 Boring counting
    Description035nowfacedatoughproblem,hisenglishteachergiveshimastring,whichconsistswithnlowercaseletter,hemustfigureouthowmanysub......
  • HDU 1828 Picture
    ProblemDescriptionAnumberofrectangularposters,photographsandotherpicturesofthesameshapearepastedonawall.Theirsidesareallvertical......
  • POJ 1837 Balance
    DescriptionGigelhasastrange"balance"andhewantstopoiseit.Actually,thedeviceisdifferentfromanyotherordinarybalance. Itorderstw......
  • 454. 四数相加 II
    454.四数相加II给你四个整数数组nums1、nums2、nums3和nums4,数组长度都是n,请你计算有多少个元组(i,j,k,l)能满足:0<=i,j,k,l<nnums1[i]+nums2......
  • PAT (Advanced Level) Practise 1118 Birds in Forest (25)
    1118.BirdsinForest(25)时间限制150ms内存限制65536kB代码长度限制16000B......
  • CSU 1807 最长上升子序列~
    [​​Submit​​​][​​​Status​​​][​​​WebBoard​​]Description1,p2,…,pn.1,p2,…,pn 互不相同(即p1,p2,…,pn......
  • CSU 1813 盖房子
    DescriptionBobo在ICPCCamp买了一块n×m的土地,其中有些格子是障碍。他想选择两个矩形区域,建造两座房子。很明显,用于盖房子的区域不能包含障碍。同时,两个......
  • CSU 1812 三角形和矩形
    DescriptionBobo有一个三角形和一个矩形,他想求他们交的面积。1,y1,x2,y2,x3,y3,x4,y4 描述。表示三角形的顶点坐标是(x1,y1),(x......
  • CSU 1808 地铁
    Description Bobo居住在大城市ICPCCamp。i 号线,位于站ai,bi 之间,往返均需要花费ti 分钟(即从ai 到bi 需要ti 分钟,从bi 到ai......