[抄题]:

Given an array S of n integers, are there elements abc 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

[输入量特别大怎么办]:

[画图]:

[一刷]:

  1. 记得第一步就写排序
  2. corner case反正都是空的,直接return res自己就行。
  3. 防止重复判断时必须要写(i > 0 && nums[i] != nums[i - 1]),因为i - 1最后一位是0。
  4. 下次不要定义value了,因为value随指针的改变而不断变化。
  5. List<List<Integer>> res = new ArrayList<List<Integer>>(); 只有最右边是空的。
  6. 循环条件是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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结

    前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...

  4. [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 ...

  5. 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 ...

  6. 16. 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. eclipse如何调试jar包源码

    转载至:http://blog.csdn.net/niclascage/article/details/47451967 引子 深入学习开源代码很多时候都需要去调试别人的jar包.当然如果你能拿别人的 ...

  2. CA单向认证和双向认证的区别?

    1:单向认证,内容会被串改吗?

  3. (转)Win7 64位系统下 Retional rose 2003 安装及破解

    网上关于Retional rose 2003安装和破解的文章比较多,这里,我结合自己的亲身体验,和大家分享一下win7 旗舰版 64位系统下Retional rose 2003(下面简称rose200 ...

  4. 通过Vim+少量插件配置一个高效简洁的IDE

    最近本人在看<TCP/IP Illustrated Volume2:The Implementation>这本书,自然要下载4.4BSD-Lite的源代码配合书本一起研读.以前学习Vim的 ...

  5. 【Python编程:从入门到实践】chapter6 字典

    chapter6 字典 6.1 一个简单的字典 6.2 使用字典 6.2.1 访问字典中的值 6.2.2 添加键值对 6.2.3 先创建一个空字典 6.2.4 修改字典中的值 6.2.5 删除键值对 ...

  6. 文件读写’r'和’rb’区别

    2012年08月22日 ⁄ 综合 ⁄ 共 849字 ⁄ 字号 小 中 大 ⁄ 评论关闭不管何种语言在进行文件读写时,大家都知道有以下模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别 ...

  7. PyQt5系列教程(七)控件

    软硬件环境 Windows 10 Python 3.4.2 PyQt 5.5.1 PyCharm 5.0.4 前言 控件是PyQt应用程序的基石.PyQt5自带很多不同的控件,包括像button.ch ...

  8. tornado-通过判断后台数据限制登陆--简单的

    import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # 提供 ...

  9. uva-10110

    走廊上的灯1-n编号,一个人走过去,第i次走的时候,如果灯编号n%i=0,那么就把这个灯的开关摁一下,从最后一个灯返回到第一个灯不做任何操作,问最后一个灯最后是关还是开, 求完全平方数, 比如8,不存 ...

  10. 20.OGNL与ValueStack(VS)-普通方法访问

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 首先在User中增加一个成员方法,代码如下: public String g ...