java 3sum_[Leetcode] 3Sum 4Sum 3Sum Closet 多数和

2Sum

在分析多数和之前,请先看Two Sum的详解

3Sum

双指针法

复杂度

时间 O(N^2) 空间 O(1)

思路

3Sum其实可以转化成一个2Sum的题,我们先从数组中选一个数,并将目标数减去这个数,得到一个新目标数。然后再在剩下的数中找一对和是这个新目标数的数,其实就转化为2Sum了。为了避免得到重复结果,我们不仅要跳过重复元素,而且要保证2Sum找的范围要是在我们最先选定的那个数之后的。

代码

public class Solution {

public List> threeSum(int[] nums) {

Arrays.sort(nums);

ArrayList> res = new ArrayList>();

for(int i = 0; i < nums.length - 2; i++){

// 跳过重复元素

if(i > 0 && nums[i] == nums[i-1]) continue;

// 计算2Sum

ArrayList> curr = twoSum(nums, i, 0 - nums[i]);

res.addAll(curr);

}

return res;

}

private ArrayList> twoSum(int[] nums, int i, int target){

int left = i + 1, right = nums.length - 1;

ArrayList> res = new ArrayList>();

while(left < right){

if(nums[left] + nums[right] == target){

ArrayList curr = new ArrayList();

curr.add(nums[i]);

curr.add(nums[left]);

curr.add(nums[right]);

res.add(curr);

do {

left++;

}while(left < nums.length && nums[left] == nums[left-1]);

do {

right--;

} while(right >= 0 && nums[right] == nums[right+1]);

} else if (nums[left] + nums[right] > target){

right--;

} else {

left++;

}

}

return res;

}

}

2019/01

Go

func threeSum(nums []int) [][]int {

// sort the slice to avoid duplicate and also use two pointers

sort.Slice(nums, func(i, j int) bool {

return nums[i] < nums[j]

})

res := [][]int{}

for i := 0; i < len(nums); i++ {

// skip duplicate numbers

if i != 0 && nums[i] == nums[i - 1] {

continue

}

// convert into a twoSum problem

res = twoSum(nums, i + 1, nums[i], res)

}

return res

}

func twoSum(nums []int, start, first int, res [][]int) [][]int {

left := start

right := len(nums) - 1

for left < right {

sum := nums[left] + nums[right]

if sum == 0 - first {

res = append(res, []int{first, nums[left], nums[right]})

left++

right--

// skip duplicate numbers from left side

for left < len(nums) && left > 0 && nums[left] == nums[left - 1] {

left++

}

// skip duplicate numbers from right side

for right >= 0 && right < len(nums) - 1 && nums[right] == nums[right + 1] {

right--

}

} else if sum > 0 - first {

right--

} else if sum < 0 - first {

left++

}

}

return res

}

4Sum

双指针法

复杂度

时间 O(N^3) 空间 O(1)

思路

和3Sum的思路一样,在计算4Sum时我们可以先选一个数,然后在剩下的数中计算3Sum。而计算3Sum则同样是先选一个数,然后再剩下的数中计算2Sum。

代码

public class Solution {

public List> fourSum(int[] nums, int target) {

Arrays.sort(nums);

ArrayList> res = new ArrayList>();

for(int i = 0; i < nums.length - 3; i++){

if(i > 0 && nums[i] == nums[i-1]) continue;

List> curr = threeSum(nums, i, target - nums[i]);

res.addAll(curr);

}

return res;

}

private List> threeSum(int[] nums, int i, int target) {

ArrayList> res = new ArrayList>();

for(int j = i + 1; j < nums.length - 2; j++){

if(j > i + 1 && nums[j] == nums[j-1]) continue;

List> curr = twoSum(nums, i, j, target - nums[j]);

res.addAll(curr);

}

return res;

}

private ArrayList> twoSum(int[] nums, int i, int j, int target){

int left = j + 1, right = nums.length - 1;

ArrayList> res = new ArrayList>();

while(left < right){

if(nums[left] + nums[right] == target){

ArrayList curr = new ArrayList();

curr.add(nums[i]);

curr.add(nums[j]);

curr.add(nums[left]);

curr.add(nums[right]);

res.add(curr);

do {

left++;

}while(left < nums.length && nums[left] == nums[left-1]);

do {

right--;

} while(right >= 0 && nums[right] == nums[right+1]);

} else if (nums[left] + nums[right] > target){

right--;

} else {

left++;

}

}

return res;

}

}

3Sum Closet

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

For example, given array S = {-1 2 1 -4}, and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

双指针法

复杂度

时间 O(N^2) 空间 O(1)

思路

和3Sum的解法一样。在3Sum中,我们只有找到和目标完全一样的时候才返回,但在Closet中,我们要记录一个最小的差值,并同时记录下这个最小差值所对应的和。

代码

public class Solution {

public int threeSumClosest(int[] nums, int target) {

Arrays.sort(nums);

int closetSum = 0, minDiff = Integer.MAX_VALUE / 2;

for(int i = 0; i < nums.length; i++){

int left = i + 1, right = nums.length - 1;

while(left < right){

// 当前组合的和

int sum = nums[i] + nums[left] + nums[right];

// 当前组合的和与目标的差值

int diff = Math.abs(sum - target);

// 如果差值更小则更新最接近的和

if(diff < minDiff){

closetSum = sum;

minDiff = diff;

}

// 双指针的移动方法和3Sum一样

if (sum < target){

left++;

} else if (sum > target){

right--;

} else {

return sum;

}

}

}

return closetSum;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值