3sum, 3sum closest
[抄题]:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
[思维问题]:
[一句话思路]:
化成和为0 - nums[i]的两根指针2sum
[输入量特别大怎么办]:
[画图]:
[一刷]:
- 记得第一步就写排序
- corner case反正都是空的,直接return res自己就行。
- 防止重复判断时必须要写(i > 0 && nums[i] != nums[i - 1]),因为i - 1最后一位是0。
- 下次不要定义value了,因为value随指针的改变而不断变化。
- List<List<Integer>> res = new ArrayList<List<Integer>>(); 只有最右边是空的。
- 循环条件是for (int i = 0; i < nums.length; i++),上界还是i < nums.length
[总结]:
不要定义value
记得先排序
[复杂度]:
[英文数据结构,为什么不用别的数据结构]:
用linkedlist: 3sum的元素个数不确定,需要动态添加
[其他解法]:
[题目变变变]:
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new LinkedList<>();
if (nums == null || nums.length < 3) {
return res;
}
Arrays.sort(nums);//!!!
for (int i = 0; i < nums.length; i++) {
int left = i + 1;
int right = nums.length - 1;
int sum = 0 - nums[i];
if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
while(left < right) {
if (nums[left] + nums[right] == sum) {
res.add(Arrays.asList(nums[i],nums[left],nums[right]));
while(left < right && nums[left] == nums[left + 1]) {
left++;
}
while(left < right && nums[right] == nums[right - 1]) {
right--;
}
left++;
right--;
}
else if (nums[left] + nums[right] < sum) {
left++;
}
else {
right--;
}
}
}
}
return res;
}
}
3sum closest
[抄题]:
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
[思维问题]:
用diff做要判断两边,很麻烦。
[一句话思路]:
用abs绝对值函数,直接就两边比较大小。
[输入量特别大怎么办]:
[画图]:
[一刷]:
用了指针的变量,要放在发生指针变化的循环体之内。
[总结]:
注意用了指针的变量
[复杂度]:
[英文数据结构,为什么不用别的数据结构]:
[其他解法]:
[Follow Up]:
[题目变变变]:
2sum closest 4...
class Solution {
public int threeSumClosest(int[] nums, int target) {
if (nums.length < 3 || nums == null) {
return -1;
}
Arrays.sort(nums);
int bestSum = nums[0] + nums[1] + nums[2];
for(int i = 0; i < nums.length; i++) {
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (Math.abs(sum - target) < Math.abs(bestSum - target)) {
bestSum = sum;
}
if (sum == target) {
while(left < right && nums[left] == nums[left + 1]) {
left++;
}
while(left < right && nums[right] == nums[right - 1]) {
right--;
}
left++;
right--;
}
else if (sum < target) {
left++;
}
else {
right--;
}
}
}
return bestSum;
}
}
3sum, 3sum closest的更多相关文章
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- Leetcode 16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 16. 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- Leetcode 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- No.016:3Sum Closest
问题: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 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 ...
随机推荐
- Django1.7开发博客
转自: http://www.pycoding.com/articles/category/django 基于最新的django1.7写的,通俗易懂,非常适合新手入门. 感谢博主! 参考教程: htt ...
- [UE4]Character,飞行模式。
Character 是pawn的子类,可以行走.跳跃.游泳.飞行的Pawn.
- 一个关于考勤统计的sql研究
在这里,我们要做一个简单的员工考勤记录查询系统的后台数据库.业务需求如下所示: 1.统计每天来的最早.来的最晚.走的最早.走得最晚的人的姓名 1.1 统计每天来得最早的人 ...
- sklearn的GridSearchCV例子
class sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_j ...
- Tornado 框架的使用
Tornado tornado是一个轻量级python的web框架,他是非阻塞式的,而且速度非常快.得利于其 非阻塞的方式和对 epoll 的运用,Tornado 每秒可以处理数以千计的连接,这意味着 ...
- Solr学习总结(六)solr的函数查询Function Queries
摘要: 函数查询允许你使用一个或多个数字字段的真实值生成一个相关性分数,函数查询在standard,DisMax,eDisMax下都能使用. 查询函数可以是常量,字段或者其他函数的组合.使用函数可以影 ...
- mysql更新(八) 可视化工具Navicat的使用 索引
17-索引 一.索引的介绍 数据库中专门用于帮助用户快速查找数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置吗,然后直接获取. 二 .索引的作用 约束和加速查 ...
- linux操作系统2 linux常用命令
知识内容: 1.目录及文件操作 2.用户.群组与权限 3.重定向.管道 4.磁盘存储管理 5.系统命令 6.其他命令 参考: http://man.linuxde.net/ Linux命令规则:目录名 ...
- uva-10112-计算几何
题意:给你一些点,求这些点组成的三角形面积最大,而且三角形内不能包含其他点 #include <iostream> #include <math.h> #include< ...
- UVA408-水的深度的伪随机数
题意: 公式sed(x) = (sed(x-1)+step)%mod,初始值sed(x)=0,输入step和mod,求解这个公式能不能生成0,1,2,3,4,5.....mod-1 解法:暴力枚举,直 ...