zoukankan      html  css  js  c++  java
  • LeetCode 18. 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: 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]
    ]
    

    题目标签:Array
      这道题目给了我们一个nums array 和一个 target, 要我们找到所有的四个数字等于target 的可能性,并且不能重复。只要做过了threeSum,那么对于这一题,只要多加一层遍历就可以了。先来看一下threeSum,找三数之和,那一题里面要找 三数之和等于0,在这里,只要等于target就可以了,遍历nums array,对于每一个数字,利用twoSum(two pointers)方法来找到后面两个数字的所有可能性。那么来看这一题,这一题需要四个数字,a+b+c+d = target。那么我们遍历nums array, 对于每一个数字,就是确定了a, 接着第二层遍历,来确定b这个数字,当确定了b,利用twoSum来找到后面两个数字,c和d。 对于每一个a,要把所有b可能性都找了,对于每一个b,要把所有c和d的可能性都找了。简而言之,多加一层for loop就可以了。为了避免重复,依然要加 如果碰到遇到过的数字,就跳过的 设置。要注意的是,既然多了一个数字a, 那么在b里面 “如果遇到重复的数字就跳过” 这个设置里重复的数字不能包括a。
     
     

    Java Solution:

    Runtime beats 63.69% 

    完成日期:07/13/2017

    关键词:Array

    关键点:利用threeSum, 只要多加一层for loop,注意修改避免重复的条件

     
     
     1 public class Solution 
     2 {
     3     public List<List<Integer>> fourSum(int[] nums, int target) 
     4     {
     5         Arrays.sort(nums);
     6         List<List<Integer>> res = new ArrayList<>();
     7         
     8         for(int i=0; i<nums.length-3; i++)
     9         {
    10             if(i > 0 && nums[i] == nums[i-1]) // if previous num is same, skip this num
    11                 continue;
    12             // find three sum
    13             for(int j=i+1; j<nums.length-2; j++) // no need to find twoSum if rest array size is only 1 or 0
    14             {
    15                 if(j-1 > i && nums[j] == nums[j-1])
    16                     continue;
    17                 // for each num, find the twoSum which equal -num in the rest array
    18                 int left = j + 1;
    19                 int right = nums.length-1;
    20                 
    21                 while(left < right)
    22                 {
    23                     int sum = nums[i] + nums[j] + nums[left] + nums[right];
    24                     if(sum == target) // find the two 
    25                     {
    26                         res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); // ascending order
    27                         left++;
    28                         right--;
    29                         
    30                         while(left < right && nums[left] == nums[left-1]) // if next num is same as this, skip this
    31                             left++;
    32                         while(left < right && nums[right] == nums[right+1])
    33                             right--;
    34                     }
    35                     else if(sum > target) // meaning need smaller sum
    36                         right--;
    37                     else // nums[left] + nums[right] < sum, meaning need larger sum
    38                         left++;
    39                 }
    40                 
    41             }
    42         }
    43         
    44         return res;
    45     }
    46 }

    参考资料:N/A

    LeetCode 算法题目列表 - LeetCode Algorithms Questions List

  • 相关阅读:
    JS系列:三元运算符与循环
    浏览器解析js和type判断数据类型
    JS系列:数据类型详细讲解
    JS系列:编程语言
    京东校招面试汇总
    有关axios的request与response拦截
    正则表达式 判断内容是否为合法的url
    H5 小代码(实时更新)
    H5 回到顶部按钮
    图片压缩(js压缩,底部有vue压缩图片依赖使用的教程链接)
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7163779.html
Copyright © 2011-2022 走看看