//三数和为0的问题。要求去重,并且输出数字有序。
public List<List<Integer>> threeSum(int[] nums)
{
Arrays.sort(nums);
List<List<Integer>> lists = new ArrayList<List<Integer>>();
//对于i去重,因为p在i后面,所以不能往后去重,可能会把p的值去掉,所以要往前去重。
for(int i = 0; i < nums.length; i ++)
{
if(i>0&&nums[i] == nums[i-1])
{
continue;
}
int p = i+1, q = nums.length - 1;
while(p < q)
{
int sum = nums[i]+nums[p]+nums[q];
if(sum == 0)
{
List<Integer> list = new ArrayList<>();
list.add(nums[i]);
list.add(nums[p]);
list.add(nums[q]);
lists.add(list);
//p去重很巧妙,先自加,然后判断之前p的值和自加后的值是不是相等,如果相等,再次自加,同时也避免的去重时和q重叠。
while(++p < q && nums[p] == nums[p-1])
{
}
while(--q > p && nums[q] == nums[q+1])
{
}
}
if(sum < 0)
{
p++;
}
if(sum > 0)
{
q--;
}
}
}
return lists;
}
//三数和最接近某个值
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int temp = 0;
int dist = Integer.MAX_VALUE;
for(int i = 0; i < nums.length; i ++)
{
if(i > 0 && nums[i]==nums[i-1])
{
continue;
}
int p = i + 1, q = nums.length-1;
while(p < q)
{
int sum = nums[i] + nums[p] + nums[q];
if(sum > target)
{
if((sum - target) < dist)
{
dist = sum - target;
temp = sum;
}
q--;
}
else if(sum < target)
{
if((target - sum) < dist)
{
dist = target - sum;
temp = sum;
}
p++;
}
else
{
return sum;
}
}
}
return temp;
}

四数和问题,感觉并不是最优解。

public class Solution {
public List<List<Integer>> fourSum(int[] num, int target) {
Arrays.sort(num);
Set<List<Integer>> hashSet = new HashSet<>();
List<List<Integer>> result = new ArrayList<>(); for (int i = 0; i < num.length; i++) {
for (int j = i + 1; j < num.length; j++) {
int k = j + 1;
int l = num.length - 1; while (k < l) {
int sum = num[i] + num[j] + num[k] + num[l]; if (sum > target) {
l--;
} else if (sum < target) {
k++;
} else if (sum == target) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(num[i]);
temp.add(num[j]);
temp.add(num[k]);
temp.add(num[l]); if (!hashSet.contains(temp)) {
hashSet.add(temp);
result.add(temp);
} k++;
l--;
}
}
}
} return result;
}
}

3Sum,4Sum问题的更多相关文章

  1. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  2. LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解

    文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. ...

  3. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  4. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  5. LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结

    前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...

  6. 3Sum & 4Sum

    3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find a ...

  7. 秒杀 2Sum 3Sum 4Sum 算法题

    2 Sum 这题是 Leetcode 的第一题,相信大部分小伙伴都听过的吧. 作为一道标着 Easy 难度的题,它真的这么简单吗? 我在之前的刷题视频里说过,大家刷题一定要吃透一类题,为什么有的人题目 ...

  8. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  9. 算法题丨3Sum Closest

    描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

随机推荐

  1. codevs1044 拦截导弹==洛谷 P1020 导弹拦截

    P1020 导弹拦截 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天 ...

  2. 【BZOJ3209】花神的数论题 数位DP

    [BZOJ3209]花神的数论题 Description 背景众所周知,花神多年来凭借无边的神力狂虐各大 OJ.OI.CF.TC …… 当然也包括 CH 啦.描述话说花神这天又来讲课了.课后照例有超级 ...

  3. MiniUI 在线示例

    引用 http://miniui.com/demo/#src=datagrid/celledit.html

  4. [转载]$(document).ready(function(){});

    转载自:http://www.cnblogs.com/king-sheng/archive/2012/01/06/2313980.html $(document).ready(function() 页 ...

  5. dbUtils 原理

    // Jdbc 的增,删, 改流程类似,只是参数不同, 因此可以向上抽取 public class Demo{ // Jdbc 的增加 public void addStu(Stu stu){ Con ...

  6. Python代码实现删除一个list里面的重复元素

    lst=[11,22,33,44,22,11,22,44] print(list(set(lst))) # 打印结果:[33, 11, 44, 22] d = {} for index,item in ...

  7. spring 登录提示 Bad credentials

    spring 日志输出:Authentication failed: password does not match stored value in spring security 3.2,检查密码发 ...

  8. 玩转type类型(牛逼克拉斯 )

    一.前言 一说起type()方法肯定有很多人都知道这不就是查看某个对象类型的嘛,其实不然,他还有更牛逼的应用------创建类 >>> type(1) <class 'int' ...

  9. JS编写类似弹出窗口样式显示层

    JSp中增加div <!-- 提交变更申请 --> <div id="changeWindow" class="easyui-window" ...

  10. Python 8 协程/异步IO

    协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来 ...