zoukankan      html  css  js  c++  java
  • 4Sum

    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.

    思路:

      双指针问题的扩展

    我的代码:

    public class Solution {
        public List<List<Integer>> fourSum(int[] num, int target) {
            List<List<Integer>> list = new ArrayList<List<Integer>>();
            if(num == null || num.length < 4)   return list;
            int len = num.length;
            Arrays.sort(num);
            for(int i = 0; i <= len - 4; i++)
            {
                if(i == 0 || !(i != 0 && num[i] == num[i-1]))
                {
                    for(int j = i + 1; j <= len - 3; j++)
                    {
                        if(j == i + 1 || !(j != i+ 1 && num[j] == num[j-1]))
                        {
                            int left = j + 1;
                            int right = len - 1;
                            while(left < right)
                            {
                                int val = num[i] + num[j] + num[left] + num[right];
                                if(val == target)
                                {
                                    List<Integer> tmpList = new ArrayList<Integer>();
                                    tmpList.add(num[i]);
                                    tmpList.add(num[j]);
                                    tmpList.add(num[left]);
                                    tmpList.add(num[right]);
                                    list.add(tmpList);
                                    left++;
                                    right--;
                                    while(left < right && num[left] == num[left-1])
                                    {
                                        left++;
                                    }
                                    while(left < right && num[right] == num[right+1])
                                    {
                                        right--;
                                    }
                                 }
                                else if(val > target)
                                {
                                    right --;
                                }
                                else 
                                    left ++;
                            }
                        }
                    }
                }
            }
            return list;
        }
    }
    View Code

    学习之处:

    • 避免重复的关键之处在于:
     while(left < right && num[left] == num[left-1])
    {
        left++;
    }
  • 相关阅读:
    现在分词做状语,到,非谓语动词
    成功和失败因素收集
    退拽原理2
    分享到(事件冒泡实例)
    滚动公告(纵向)
    RabbitMQ消息队列(一): Detailed Introduction 详细介绍
    在IDEA中实战Git
    深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
    MySQL中日期和时间戳互相转换的函数和方法
    【Docker】 windows10 docker 使用
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4385355.html
Copyright © 2011-2022 走看看