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 ...
随机推荐
- MySQL-库的操作
05-库的操作 本节重点: 掌握库的增删改查 一.系统数据库 执行如下命令,查看系统库 show databases; nformation_schema: 虚拟库,不占用磁盘空间,存储的是数 ...
- 【BZOJ3239】Discrete Logging BSGS
[BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...
- java 多种判断key是否在map中存在的方法
java 中有时候会遇到判断传过来的map里是否包含了指定的key,我目前只发现两种办法,如果有其他方法欢迎补充 我添加上去: HashMap map = new HashMap(); map.put ...
- Jquery来对form表单提交(mvc方案)
来自:http://www.cnblogs.com/lmfeng/archive/2011/06/18/2084325.html 我先说明一下,这是asp.net mvc 里面的用法, Jquery来 ...
- 如何定义 match 常量?
namespace MathConstants { const double E = 2.71828182845904523536; // e const double LOG2E = 1.44269 ...
- KVM WEB管理工具webvirtmgr安装和使用
生产环境的KVM宿主机越来越多,需要对宿主机的状态进行调控.这里用webvirtmgr进行管理.图形化的WEB,让人能更方便的查看kvm 宿主机的情况和操作 1 安装支持的软件源 yum -y ins ...
- 流畅的python python 序列
内置序列 容器类型 list .tuple和collections.deque这些序列能放入不同的类型的数据 扁平序列 str.byets.bytearray.memoryview(内存视图)和arr ...
- LinuxCentos系统安装Nginx过程记录
网站服务 想必我们大多数人都是通过访问网站而开始接触互联网的吧.我们平时访问的网站服务就是Web网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务. Web网络服务是一种被动访问的服务程 ...
- yum命令的实例
1) 自定义yum仓库:createrepo 2) 自定义repo文件 3) 使用yum命令安装httpd软件包(在这里需要强调一点,本身执行yum.repos.d时,文件里面是有自带的yum源的,需 ...
- 2014牡丹江——Known Notation
题目链接 题意: 输入一个长度不超过1000的字符串,包含数字(1-9)和星号(*).字符串中的空格已经丢失,所以连起来的数字串能够看成很多分开的数.也能够看成连续的数,即能够随意加入空格. 如今有两 ...