【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.

解题分析:

这个和3Sum那道题有些像,也是要先确定两个数,之后两个数的确定可以参考二分查找的实现方法进行优化。

具体代码:

 1 public class Solution {
 2     public static List<List<Integer>> fourSum(int[] nums, int target) {
 3         Arrays.sort(nums);
 4         //边界情况的判断
 5         List<List<Integer>> results = new ArrayList<List<Integer>>();
 6         if(nums.length<4){
 7             return results;
 8         }
 9         if(nums.length==4){
10             if(nums[0]+nums[1]+nums[2]+nums[3]==target){
11                 List<Integer> result = new ArrayList<Integer>();
12                 result.add(nums[0]);
13                 result.add(nums[1]);
14                 result.add(nums[2]);
15                 result.add(nums[3]);
16                 results.add(result);
17
18             }
19             return results;
20         }
21         //用遍历方式确定前两个数的位置
22         for(int first=0;first<nums.length-3;first++){
23             for(int second=first+1;second<nums.length-2;second++){
24                 //第三个数和第四个数用二分查找确定
25                 int third=second+1;
26                 int fourth=nums.length-1;
27                 while(third<fourth){
28                     if(nums[first]+nums[second]+nums[third]+nums[fourth]>target){
29                         fourth--;
30                     }
31                     else if(nums[first]+nums[second]+nums[third]+nums[fourth]<target){
32                         third++;
33                     }
34                     else{
35                         List<Integer> result = new ArrayList<Integer>();
36                         result.add(nums[first]);
37                         result.add(nums[second]);
38                         result.add(nums[third]);
39                         result.add(nums[fourth]);
40                         results.add(result);
41                         //避免元素重复
42                         while(third<nums.length-1&&nums[third+1]==nums[third]){
43                             third++;
44                         }
45                         third++;
46                         //避免元素重复
47                         while(third<fourth&&nums[fourth-1]==nums[fourth]){
48                             fourth--;
49                         }
50                         fourth--;
51
52                     }
53                 }
54                 //避免元素重复
55                 while(second<nums.length-2&&nums[second+1]==nums[second]){
56                     second++;
57                 }
58             }
59             //避免元素重复
60             while(first<nums.length-3&&nums[first+1]==nums[first]){
61                 first++;
62             }
63         }
64         return results;
65     }
66 }
时间: 2024-08-06 01:21:20

【leetcode】18. 4Sum的相关文章

【LeetCode】18. 4Sum 解题小结

题目: Given an array S of n integers, are there elements a, b, c, 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 ex

【LeetCode】018 4Sum

题目: Given an array S of n integers, are there elements a, b, c, 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 ex

【LeetCode】454 4Sum II

题目: Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in t

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【leetcode】Candy

There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more candies

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

【LeetCode】119 - Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? Solution: 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex){

【leetcode】 First Missing Positive

[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 找到第一个没有出现的正整数 思路:

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1