3Sum,4Sum问题
//三数和为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问题的更多相关文章
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- LeetCode Two Sum&Two Sum II - Input array is sorted&3Sum&4Sum 一锅煮题解
文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. ...
- 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 ...
- 2Sum,3Sum,4Sum,kSum,3Sum Closest系列
1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- 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 ...
- 秒杀 2Sum 3Sum 4Sum 算法题
2 Sum 这题是 Leetcode 的第一题,相信大部分小伙伴都听过的吧. 作为一道标着 Easy 难度的题,它真的这么简单吗? 我在之前的刷题视频里说过,大家刷题一定要吃透一类题,为什么有的人题目 ...
- [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 ...
- 算法题丨3Sum Closest
描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
随机推荐
- [Spring MVC]学习笔记--form表单标签的使用
github例子地址: https://github.com/lemonbar/spring-mvc-jsp 效果图 关于spring mvc的标签的讲解, 有一篇blog已经讲的很细了. http: ...
- C++ 基础知识回顾(I/O)
[1] I/O基础 大多数计算机语言的输入输出的实现都是以语言本身为基础的,但是C/C++没有这样做.C语言最初把I/O留给了编译器实现人员.这样做的一个原因是可以提供足够的自由度,使之最适合目标机器 ...
- 1674 区间的价值 V2(分治)
1674 区间的价值 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 lyk拥有一个区间. 它规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有 ...
- mysql编译参数详解(./configure)
1.--prefix=PREFIX:指定程序安装路径: 2.--enable-assembler:使用汇编模式:(文档说明:compiling in x86 (and sparc) versions ...
- margin 依附与可见的内容,不能为margin而写margin
margin 依附于可见的内容,不能为margin而写margin <!DOCTYPE html><html><head><style> p{marg ...
- Linux彻底删除mysql5.6
查看安装的mysql组件 rpm -qa | grep -i mysql mysql57-community-release-el6-8.noarch mysql-community-common-5 ...
- Asp.Net MVC anti-forgery token的问题:nameidentifier or identityprovider not present
当使用ClaimsIdentity的时候,Asp.Net MVC在生成AntiForgeryToken的时候会默认使用User.Identity中两种ClaimsType的值:NameIdentifi ...
- matlab学习笔记之基础知识(一)
一.两种特殊数据类型 1.元胞数组 元胞数组是MATLAB的一种特殊数据类型,可以将元胞数组看做一种无所不包的通用矩阵,或者叫做广义矩阵.组成元胞数组的元素可以是任何一种数据类型的常数或者常量,每 ...
- Map.Entry<K,V>分析
一.好处 你是否已经对每次从Map中取得关键字然后再取得相应的值感觉厌倦? Set keys = map.keySet( ); if(keys != null) { Iterator iterator ...
- uwsgi+nginx项目上线
一.基础环境配置 1.Linux安装配置 1.设置IP地址 [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 ...