18 & 454. 4Sum I & II

18. 4Sum I (32lines)

Given a integer array and target, find all unique quadruplets sum is target.

Solution(O(N^3)):

Similar as 3sum problem, add one more loop and be careful about the duplicates.

 1 public class Solution {
 2     public List<List<Integer>> fourSum(int[] nums, int target) {
 3         Arrays.sort(nums);
 4         List<List<Integer>> result = new ArrayList<List<Integer>>();
 5         for(int i = 0; i < nums.length - 3; i++) {
 6             int sum3 = target - nums[i];
 7             for(int j = i+1; j < nums.length - 2; j++) {
 8                 int sum2 = sum3 - nums[j];
 9                 int lo = j+1, hi = nums.length - 1;
10                 while(lo < hi) {
11                     int sum = nums[i] + nums[j] + nums[lo] + nums[hi];
12                     if(sum == target) {
13                         List<Integer> item = new ArrayList<Integer>();
14                         item.add(nums[i]);
15                         item.add(nums[j]);
16                         item.add(nums[lo]);
17                         item.add(nums[hi]);
18                         result.add(item);
19                         while(lo < hi && nums[lo] == nums[lo+1]) lo++;
20                         while(lo < hi && nums[hi] == nums[hi-1]) hi--;
21                         lo++;hi--;
22                     }
23                     else if(sum > target) hi--;
24                     else lo++;
25                 }
26                 while(j < nums.length - 2 && nums[j] == nums[j + 1]) j++;
27             }
28             while(i < nums.length - 3 && nums[i] == nums[i + 1]) i++;
29         }
30         return result;
31     }
32 }

454. 4Sum II

Given 4 same-length arrays, find how many pairs of a[i] + b[j] + c[k] + d[l] = 0

Solution(O(N^2)):

Not same problem as the 2,3,4 sum. From different arrays, and can use hashmap, and make it a similar problem as the unordered 2 sum.

 1 ublic class Solution {
 2     public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
 3         int count = 0;
 4         int length = A.length;
 5         //sort is used and faster
 6         Arrays.sort(A);
 7         Arrays.sort(B);
 8         Arrays.sort(C);
 9         Arrays.sort(D);
10         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
11         for(int i = 0; i < length; i++) {
12             for(int j = 0; j < length; j++) {
13                 int sum = A[i] + B[j];
14                 map.put(sum, map.getOrDefault(sum, 0) + 1);
15             }
16         }
17
18         for(int k = 0; k < length; k++) {
19             for(int l = 0; l < length; l++) {
20                 int sum = C[k] + D[l];
21                 if(map.containsKey(-sum)) {
22                     count += map.get(-sum);
23                 }
24             }
25         }
26         return count;
27     }
28 }
时间: 2024-11-03 12:25:54

18 & 454. 4Sum I & II的相关文章

LeetCode 454. 4Sum II

454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18801 Difficulty: Medium Contributors: Samuri Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[

[LeetCode] 454. 4Sum II 四数之和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 the r

【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

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 the r

8.18 [LeetCode 52] N-Queens II

[LeetCode 52] N-Queens II | COMMENTS Question link Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. Stats Frequency 3 Difficulty 4 Adjusted Difficulty 2 Time to use ——– Ratin

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: Elements in a quadruplet (a,b,c,d) must be in non-descending ord

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea