N sum 系列 15,16,918,923,18,454
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]]
Example 2:
Input: nums = [] Output: []
Example 3:
Input: nums = [0] Output: []
Constraints:
0 <= nums.length <= 3000-105 <= nums[i] <= 105
解法: 3sum 说白了可以转换为2sum问题,多加一层循环而已
class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> result = new ArrayList(); for(int i=0;i<nums.length-2;i++){ if(i>0 && nums[i]==nums[i-1]) continue; //针对重复过滤 int l =i+1,r = nums.length-1;
//twoSum的过程 while(l<r){ int sum = nums[i]+nums[l]+nums[r]; if(sum>0) r--; else if(sum<0) l++; else{ result.add(Arrays.asList(nums[i],nums[l],nums[r])); l++;r--; } while(l>i+1 && l<r && nums[l]==nums[l-1]) l++; //针对重复过滤 while(r<nums.length-1 && r>l && nums[r]==nums[r+1]) r--;//针对重复过滤 } } return result; } }
时间复杂度: O(N2)
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
Return the sum of the three integers.
You may assume that each input would have exactly one solution.
Example 1:
Input: nums = [-1,2,1,-4], target = 1 Output: 2 Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Example 2:
Input: nums = [0,0,0], target = 1 Output: 0
Constraints:
3 <= nums.length <= 1000-1000 <= nums[i] <= 1000-104 <= target <= 104
class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int min = 10000; int result = 0; for(int i=0;i<nums.length-2;i++){ if(i>0 && nums[i]==nums[i-1]) continue; //针对重复加速 int l =i+1,r = nums.length-1; while(l<r){ int sum = nums[i]+nums[l]+nums[r]; if(min>Math.abs(sum-target)){ min = Math.abs(sum-target); result = sum; } if(sum>target) r--; else if(sum<target) l++; else return result; while(l>i+1 && l<r && nums[l]==nums[l-1]) l++; //针对重复加速 while(r<nums.length-1 && r>l && nums[r]==nums[r+1]) r--;//针对重复加速 } } return result; } }
时间复杂度: O(N2)
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.
Example1
Input: nums = [-2,0,1,3], target = 2
Output: 2
Explanation:
Because there are two triplets which sums are less than 2:
[-2, 0, 1]
[-2, 0, 3]
Example2
Input: nums = [-2,0,-1,3], target = 2
Output: 3
Explanation:
Because there are three triplets which sums are less than 2:
[-2, 0, -1]
[-2, 0, 3]
[-2, -1, 3]
Could you solve it in O(n2) runtime?
public class Solution { public int threeSumSmaller(int[] nums, int target) { Arrays.sort(nums); int count = 0; for(int i=0;i<nums.length-2;i++){ int l = i+1; int r = nums.length-1; while(l<r){ if(nums[i]+nums[l]+nums[r]>=target) r--; else{ //这里如果当前满足条件,那么r作为l+1~r 都满足条件 count += r-l; l++; } } } return count; } }
时间复杂度: O(N2)
Given an integer array arr, and an integer target, return the number of tuples i, j, k such that i < j < k and arr[i] + arr[j] + arr[k] == target.
As the answer can be very large, return it modulo 109 + 7.
Example 1:
Input: arr = [1,1,2,2,3,3,4,4,5,5], target = 8 Output: 20 Explanation: Enumerating by the values (arr[i], arr[j], arr[k]): (1, 2, 5) occurs 8 times; (1, 3, 4) occurs 8 times; (2, 2, 4) occurs 2 times; (2, 3, 3) occurs 2 times.
Example 2:
Input: arr = [1,1,2,2,2,2], target = 5 Output: 12 Explanation: arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times: We choose one 1 from [1,1] in 2 ways, and two 2s from [2,2,2,2] in 6 ways.
Constraints:
3 <= arr.length <= 30000 <= arr[i] <= 1000 <= target <= 300
class Solution { public int threeSumMulti(int[] arr, int target) { long[] dic = new long[101]; for(int ele:arr) dic[ele]++; Set<Integer> set = new HashSet(); for(int i=0;i<101;i++) if(dic[i]>0) set.add(i); long count =0; long mod = 1000000007; for(int i:set){ for(int j:set){ int temp = target-i-j; if(set.contains(temp)){ if(i==j && i==temp){ count+=dic[i]*(dic[i]-1)*(dic[i]-2)/6; } else if(i==j && i!=temp){ count+=dic[i]*(dic[i]-1)*dic[temp]/2; } else if(i<j && j<temp){ //这个地方要注意,不加条件的话会导致重复计算 count+=dic[i]*dic[j]*dic[temp]; } count = count%mod; } } } return (int)count; } }
时间复杂度: O(M2)
Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
0 <= a, b, c, d < na,b,c, anddare distinct.nums[a] + nums[b] + nums[c] + nums[d] == target
You may return the answer in any order.
Example 1:
Input: nums = [1,0,-1,0,-2,2], target = 0 Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2:
Input: nums = [2,2,2,2,2], target = 8 Output: [[2,2,2,2]]
Constraints:
1 <= nums.length <= 200-109 <= nums[i] <= 109-109 <= target <= 109
解法: 4sum 说白了可以换为 3sum问题,多加一层循环而已
class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); List<List<Integer>> result = new ArrayList(); for( int i=0;i<nums.length-3;i++ ){ if(i>0 && nums[i]==nums[i-1]) continue;//去重 for(int j=i+1;j<nums.length-2;j++){ if(j>i+1 && nums[j]==nums[j-1]) continue;//去重 int l=j+1,r=nums.length-1; while(l<r){ if(nums[i]+nums[j]+nums[l]+nums[r]<target) l++; else if(nums[i]+nums[j]+nums[l]+nums[r]>target) r--; else{ result.add(Arrays.asList(nums[i],nums[j],nums[l],nums[r])); l++;r--; } while(l<r && l>j+1 && nums[l]==nums[l-1]) l++; //去重 while(l<r && r<nums.length-1 && nums[r]==nums[r+1]) r--;//去重 } } } return result; } }
class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); return kSum(nums, target, 4, 0); } private List<List<Integer>> kSum(int[] nums, long target, int k, int start){ int len = nums.length; if(k == 2){ return twoSum(nums, target, 2, start); } else{ List<List<Integer>> list = new ArrayList(); for(int i = start; i < len - k + 2; i++){ if(i != start && nums[i] == nums[i - 1]) continue; for(List<Integer> cList : kSum(nums, target - nums[i], k - 1, i + 1)){ List<Integer> temp = new ArrayList(); temp.add(nums[i]); temp.addAll(cList); list.add(temp); } } return list; } } private List<List<Integer>> twoSum(int[] nums, long target, int k, int start){ List<List<Integer>> list = new ArrayList(); int len = nums.length; int left = start, right = len - 1; while(left < right){ int sum = nums[left] + nums[right]; if(sum == target) list.add(Arrays.asList(nums[left], nums[right])); if(sum < target) left++; else right--; while(left < right && left > start && nums[left] == nums[left - 1]) left++; while(left < right && right < len - 1 && nums[right] == nums[right + 1]) right--; } return list; } }
Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that:
0 <= i, j, k, l < nnums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
Example 1:
Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2] Output: 2 Explanation: The two tuples are: 1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0 2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
Example 2:
Input: nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0] Output: 1
Constraints:
n == nums1.lengthn == nums2.lengthn == nums3.lengthn == nums4.length1 <= n <= 200-228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228
class Solution { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { Map<Integer,Integer> map = new HashMap(); for(int i=0;i<nums1.length;i++){ for(int j=0;j<nums2.length;j++){ map.put(nums1[i]+nums2[j],map.getOrDefault(nums1[i]+nums2[j],0)+1); } } int count = 0; for(int i=0;i<nums3.length;i++){ for(int j=0;j<nums4.length;j++){ count += map.getOrDefault(0-nums3[i]-nums4[j],0); } } return count; } }

浙公网安备 33010602011771号