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 ...
随机推荐
- matlab下将图片序列转化为视频文件 && 将为视频文件转化图片序列
将图片序列转化为视频文件 程序如下: framesPath = 'E:\img\';%图像序列所在路径,同时要保证图像大小相同 videoName = 'Bolt.avi';%表示将要创建的视频文件的 ...
- android studio 简介 (上)
自从android官方宣布不再提供eclipse adt的更新之后,android studio的推进速度超乎想象得快,不管是github上的源码分享,还是stackoverflow上的问题提问,几乎 ...
- PS制作gif动图教程
之前做过一些动图,时间久了就忘记了,每次心血来潮想做的时候又要重新找资料,网上的教程都不够完整,因此整理了一份完整的教程,针对PS新手(对,没错,就是博主自己哈哈). 准备工作:photoshop.图 ...
- 如何折叠IntelliJ IDEA代码片段
在 IntelliJ IDEA 中您可以折叠(fold)代码片段,将它们缩小到单个可见行.这样,您可以隐藏当前看起来无关紧要的细节.如果必要的话,折叠的代码片段可以被展开(unfolded). 折 ...
- dos批处理文件中的变量小结
很多情况下我们需要用到一些批处理文件中的变量,方便我们执行一些操作,这里简单整理下,方便需要的朋友 批处理中的变量,我把他分为两类,分别为"系统变量"和"自定义变量& ...
- jap_spring
jar包 applicationContent.xml <?xml version="1.0" encoding="UTF-8"?> <bea ...
- JSON.Stringify()和JSON.parse()的比较使用
1. JSON.Stringify() 将一个对象解析成字符串 <script> function myonclick() { var value = $('select option: ...
- 让旧版本的 Flash IDE 支持更新的 Flash Player/AIR 功能
转载:https://blog.zengrong.net/post/1568.html 让旧版本的 Flash IDE 支持更新的 Flash Player/AIR 功能 今天在论坛上看到一篇文章:H ...
- 使用sqldeveloper连接服务器端数据库
- tensorflow笔记之滑动平均模型
tensorflow使用tf.train.ExponentialMovingAverage实现滑动平均模型,在使用随机梯度下降方法训练神经网络时候,使用这个模型可以增强模型的鲁棒性(robust),可 ...