//三数和为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. 浅谈Spring框架注解的用法分析

    原文出处: locality 1.@Component是Spring定义的一个通用注解,可以注解任何bean. 2.@Scope定义bean的作用域,其默认作用域是”singleton”,除此之外还有 ...

  2. 字符串 转 时间戳 unix_timestamp('2018-07-01 0:0:0')

    SELECT FROM_UNIXTIME(createtime,'%Y%m%d') AS d, s.app,SUM(o.val) FROM orders o LEFT JOIN app_type s ...

  3. Vue中watch的简单应用

    Vue.js 有一个方法 watch,它可以用来监测Vue实例上的数据变动. 如果对应一个对象,键是观察表达式,值是对应回调,值也可以是方法名,或者是对象,包含选项. 下面写两个demo,参考demo ...

  4. PHP判断是手机端访问还是PC端访问网站

    Mobile_Detect 是一个轻量级的开源移动设备(手机)检测的 PHP Class, 它使用 User-Agent 中的字符串,并结合 HTTP Header,来检测移动设备环境. 这个设备检测 ...

  5. centos安装lumen

    刚开始安装报错,我用的是php7,先安装zip,uzip扩展 yum install zip unzip php7.0-zip 然后通过 Composer 的 create-project 命令来安装 ...

  6. document write & close

    在载入页面后,浏览器输出流自动关闭.在此之后,任何一个对当前页面进行操作的document.write()方法将打开—个新的输出流,它将清除当前页面内容. 必须确保调用document.close() ...

  7. Linux中的awk命令

    awk '条件1{动作1} 条件2{动作2} ...'  文件名 条件: BEGIN          在处理文件里的第一行数据之前执行 END              在处理完文件里的最后一行数据 ...

  8. 001-Eclipse、idea集成javap查看字节码、javap说明

    一.概述 分析java语言特性的一个好帮手是使用javap工具查看java编译后的字节码,如何在eclipse中配置javap工具快速查看java字节码. 二.Eclipse集成javap查看字节码 ...

  9. 0102-使用 API 网关构建微服务

    一.移动客户端如何访问这些服务 1.1.客户端与微服务直接通信[很少使用] 从理论上讲,客户端可以直接向每个微服务发送请求.每个微服务都有一个公开的端点(https ://.api.company.n ...

  10. springMVC文件的上传与下载

    1.文件上传 springmvc中只需要配置上传组件,然后配合使用MultipartFile,就可以轻松实现单个文件上传和批量上传,而且上传的文件类型和大小都可以在springmvc 配置文件中配置. ...