zoukankan      html  css  js  c++  java
  • LeetCode-018-四数之和

    四数之和

    题目描述:给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

    注意:答案中不可以包含重复的四元组。

    示例说明请见LeetCode官网。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/4sum/
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    解法一:双指针法

    首先,将nums排序;然后firstfourth指针分别从数组的第一个和最后一位开始,secondthird指针分别从first+1fourth-1处从两边向内移动,直到second不小于third,移动过程中需要判断4个指针所指向的数字之和是否和target相等,如果相等,则放到结果集result里面。 直到遍历到first不小于fourth-2为止,最后返回结果result

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Solution {
    
    
        public static List<List<Integer>> fourSum(int[] nums, int target) {
            if (nums == null || nums.length < 4) {
                return new ArrayList<>();
            }
            if (nums.length == 4 && nums[0] + nums[1] + nums[2] + nums[3] != target) {
                return new ArrayList<>();
            }
            Arrays.sort(nums);
            List<List<Integer>> result = new ArrayList<>();
            for (int first = 0, fourth = nums.length - 1; first < fourth - 2; first++) {
                // 过滤掉重复的
                if (first > 0 && nums[first] == nums[first - 1]) {
                    continue;
                }
    
                while (first < fourth - 2) {
                    // 过滤掉重复的
                    if (fourth < nums.length - 1 && nums[fourth] == nums[fourth + 1]) {
                        fourth--;
                        continue;
                    }
                    for (int second = first + 1, third = fourth - 1; second < third; second++) {
                        // 过滤掉重复的
                        if (second > first + 1 && nums[second] == nums[second - 1]) {
                            continue;
                        }
                        while (second < third && nums[first] + nums[second] + nums[third] + nums[fourth] > target) {
                            third--;
                        }
                        if (second != third && nums[first] + nums[second] + nums[third] + nums[fourth] == target) {
                            result.add(new ArrayList<>(Arrays.asList(new Integer[]{nums[first], nums[second], nums[third], nums[fourth]})));
                        }
                    }
                    fourth--;
                }
                fourth = nums.length - 1;
            }
    
            return result;
        }
    
        public static void main(String[] args) {
            int[] nums = new int[]{-3, -1, 0, 2, 4, 5};
            for (List<Integer> integers : fourSum(nums, 0)) {
                for (Integer integer : integers) {
                    System.out.print(integer + "/");
                }
                System.out.println();
            }
        }
    }
    

    【每日寄语】忠实的守住自己最初的梦想,让生活的每一天都变得有意义。

  • 相关阅读:
    C#小常识集锦(一)《更锋利的CSharp代码》读书笔记
    QueryBuilder 优雅的Linq To SQL动态查询(转)
    了解 NoSQL 的必读资料
    微软报表
    linq to sql 中,如何解决多条件查询问题,答案,用表达式树! (下)
    LINQ体验(17)——LINQ to SQL语句之动态查询
    细数那些运行在微软平台上的NoSQL数据库
    5道经典的程序题
    打造自己的LINQ Provider(中):IQueryable和IQueryProvider (转 李会军)
    Expression Examples :Report Builder
  • 原文地址:https://www.cnblogs.com/kaesar/p/14924416.html
Copyright © 2011-2022 走看看