//三数和为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. POJ3487[稳定婚姻]

    The Stable Marriage Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2974 Accepted ...

  2. Convert.ToInt32(string '000000003') 变成了 3

    Convert.ToInt32(string '000000003') 变成了 3 但是在查询的时候需要用的是string 这里的convert.toint32 反而起了坏作用,不是所有的时候都要用c ...

  3. 《从零开始学Swift》学习笔记(Day 25)——类和结构体定义

    原创文章,欢迎转载.转载请注明:关东升的博客 Swift中的类和结构体定义的语法是非常相似的.类使用class关键词定义类,使用struct关键词定义结构体,它们的语法格式如下: class 类名 { ...

  4. client-server model peer-to-peer architecture 主从式架构

    w https://zh.wikipedia.org/wiki/主从式架构 主从式架构 (Client–server model) 或客户端-服务器(Client/Server)结构简称C/S结构,是 ...

  5. lodash的使用

    Lodash是一个一致性.模块化.高性能的 JavaScript 实用工具库,内部封装了很多字符串.数组.对象等常见数据类型的处理函数. 为什么选择 Lodash ? Lodash 通过降低 arra ...

  6. Navicat for MySQL远程连接虚拟机

    在虚拟机中进入mysql mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT O ...

  7. IDEA中创建maven web项目的详细部署

    步骤一:首先先创建一个project,上次我说过了创建一个project就是一个工作空间,在这里就是创建一个maven的工作空间 步骤二:你要选择maven然后按照下面图片 的指示操作就可以了---& ...

  8. 通过实例来分析I2C基本通信协议

    本文旨在用最通俗易懂的方式.让大家明确I2C通信的过程到底是怎么回事. I2C起源于飞利浦公司的电视设计,但之后朝通用路线发展,各种电子设计都有机会用到I2C 总的来说,I2C能够简单归纳为,两根线, ...

  9. 使用Kotlin开发Android应用(IV):自定义视图和Android扩展

    在读完扩展函数和默认值这篇文章之后,那么接下来要介绍什么呢?在本系列第一篇文章中我们说过,Kotlin使得Android开发更加简单,本文我们将进一步作介绍. 自定义视图 你应该还记得,在说到Kotl ...

  10. mysql-5.7.16-linux-glibc2.5-x86_64精简后的主从配置

    1.创建复制账号,并授予复制权限CREATE USER 'fansik'@'10.%' IDENTIFIED BY 'fansik';GRANT REPLICATION SLAVE ON *.* TO ...