4 Sum leetcode java

题目:

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:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, abcd)
  • 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)
题解:4 sum跟3 sum是一样的思路,只不过需要多考虑一个加数,这样时间复杂度变为O(n3)。

使用HashSet来解决重复问题的代码如下:

1 public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
 2     HashSet<ArrayList<Integer>> hashSet = new HashSet<ArrayList<Integer>>();
 3     ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
 4     Arrays.sort(num);
 5     for (int i = 0; i <= num.length-4; i++) {
 6         for (int j = i + 1; j <= num.length-3; j++) {
 7             int low = j + 1;
 8             int high = num.length - 1;
 9  
10             while (low < high) {
11                 int sum = num[i] + num[j] + num[low] + num[high];
12  
13                 if (sum > target) {
14                     high--;
15                 } else if (sum < target) {
16                     low++;
17                 } else if (sum == target) {
18                     ArrayList<Integer> temp = new ArrayList<Integer>();
19                     temp.add(num[i]);
20                     temp.add(num[j]);
21                     temp.add(num[low]);
22                     temp.add(num[high]);
23  
24                     if (!hashSet.contains(temp)) {
25                         hashSet.add(temp);
26                         result.add(temp);
27                     }
28  
29                     low++;
30                     high--;
31                 }
32             }
33         }
34     }
35  
36     return result;
37 }

使用挪动指针的方法来解决重复的代码如下:

1 public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
 2     HashSet<ArrayList<Integer>> hashSet = new HashSet<ArrayList<Integer>>();
 3     ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
 4     Arrays.sort(num);
 5     for (int i = 0; i <= num.length-4; i++) {
 6         if(i==0||num[i]!=num[i-1]){
 7             for (int j = i + 1; j <= num.length-3; j++) {
 8                 if(j==i+1||num[j]!=num[j-1]){
 9                     int low = j + 1;
10                     int high = num.length - 1;
11          
12                     while (low < high) {
13                         int sum = num[i] + num[j] + num[low] + num[high];
14          
15                         if (sum > target) {
16                             high--;
17                         } else if (sum < target) {
18                             low++;
19                         } else if (sum == target) {
20                             ArrayList<Integer> temp = new ArrayList<Integer>();
21                             temp.add(num[i]);
22                             temp.add(num[j]);
23                             temp.add(num[low]);
24                             temp.add(num[high]);
25          
26                             if (!hashSet.contains(temp)) {
27                                 hashSet.add(temp);
28                                 result.add(temp);
29                             }
30          
31                             low++;
32                             high--;
33                             
34                             while(low<high&&num[low]==num[low-1])//remove dupicate
35                                 low++;
36                             while(low<high&&num[high]==num[high+1])//remove dupicate
37                                 high--;
38                         }
39                     }
40                 }
41             }
42         }
43     }
44  
45     return result;
46 }

4 Sum leetcode java

时间: 2024-08-22 01:39:52

4 Sum leetcode java的相关文章

Path Sum leetcode java

题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true,

Combination Sum leetcode java

题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) w

3 Sum leetcode java

题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s

Binary Tree Maximum Path Sum leetcode java

题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. 题解: 递归求解. 取当前点和左右边加和,当前点的值中最大的作为本层返回值.并在全局维护一个max.使用数组,因为是引用类型.所以在递归过程中可以保存结果. 代码如下: 1

Two Sum Leetcode Java

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2

Combination Sum II leetcode java

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

Sum Root to Leaf Numbers leetcode java

题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2

Path Sum II leetcode java

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 题解: 这道题除了要判断是否有这样的一个p

Triangle leetcode java

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1