zoukankan      html  css  js  c++  java
  • leetcode--4Sum

    Given an array S of n integers, are there elements abc, 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:

    • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
    • 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)

    The algorithm is similar to 3Sum.
    public class Solution {
        public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
            ArrayList<ArrayList<Integer> > result = new ArrayList<ArrayList<Integer> >();
            int len = num.length;
            if(len >=4){
                // sort array first
                Arrays.sort(num); 
                for(int i = 0; i < len - 3; ++i){
                    int j = i + 1;
                    int sum = target - num[i];
                    while(j < len - 2){
                        int mid = j + 1; 
                        int end = len - 1;
                        while(mid < end){
                            int tripleSum = num[j] + num[mid] + num[end];
                            if(tripleSum == sum){
                                ArrayList<Integer> oneSolution = new ArrayList<Integer>();
                                oneSolution.add(num[i]);
                                oneSolution.add(num[j]);
                                oneSolution.add(num[mid]);
                                oneSolution.add(num[end]);
                                result.add(oneSolution);                        
                            }
                            else if(tripleSum < sum){
                                ++mid;
                                continue;
                            }
                            else{
                                --end;
                                continue;
                            }                    
                            ++mid;
                            --end;
                            while(mid < end && num[mid - 1] == num[mid])
                                ++mid;
                            while(mid < end && num[end] == num[end + 1])
                                --end;
                        }
                        ++j;
                        while(j < len - 2 && num[j - 1] == num[j])
                            ++j;
                    }
                    while(i < len - 3 && num[i] == num[i + 1])
                        ++i;            
                }
            }
            return result;    
        }
    }
    

      

  • 相关阅读:
    【转】sql server编写通用脚本自动检查两个不同服务器的新旧数据库的表结构差异
    Pytest 2
    【转】python通过SMTP协议发送邮件失败,报错505或535
    【转】环境搭建之allure的安装配置,及简单使用
    Pytest 1
    替换姓名为隐式
    docker 用户组权限
    安装go环境
    Win10配置WSL2安装Ubuntu,并支持Nvidia CUDA 环境
    miniconda源配置
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3617061.html
Copyright © 2011-2022 走看看