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 ...
随机推荐
- 1307 绳子与重物(DFS)
1307 绳子与重物 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N条绳子编号 0 至 N - 1,每条绳子后面栓了一个重物重量 ...
- JS实现当鼠标移动到图片上时,时图片旋转
<div id="container" style="width:500px;height:400px;position:relative;margin:0 aut ...
- SQL Server函数
阅读目录 SQL Server函数---Union与Union All的区别 回到顶部 SQL Server函数---Union与Union All的区别 如果我们需要将两个select语句的结果作为 ...
- 小程序 less wxss 混合 Mixins picker样式优化 箭头样式的实现原理
lessc src/style/picker-arrow_.less src/style/picker-arrow_.wxss 快速入门 | Less.js 中文文档 https://less.boo ...
- JavaScript方法splice()和slice()
1 splice() 1.1 说明 splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目.该方法会改变原始数组.Link 1.2 语法 arrayObject.splice(inde ...
- setlocale()函数测试当前语言的两个程序
http://www.cnblogs.com/cnyao/archive/2010/05/06/1729220.html setlocale()函数是用来配置地域信息的,原本以为这个也是windows ...
- 我的Android进阶之旅------>Android关于dp(dip)、sp转px的工具类
下面是一个工具类,提供了dp.sp.px之间相互转化的方法. import android.content.Context; /** * dp.sp 转换为 px 的工具类<br> * & ...
- python requests 使用
快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...
- OC、C#与JAVA语法特点一些异同(差集&交集)
C#对JAVA: 1.扩展方法 2.部分类 3.动态对象 4.匿名返回类型 5.表达式树 6.Linq 7.没有函数指针,委托,事件的直接提供方式 8.JAVA接口不规定以I开头,这个很烂的思想! J ...
- LATEX教程(一)
第一个文档 打开WinEdt,建立一个新文档,将以下内容复制进入文档中,保存,保存类型选择为UTF-8. \documentclass{article} \begin{document} hello, ...