Question:

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.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},A solution set is:(-1, 0, 1)(-1, -1, 2)

Analysis:

在sum系列中,这是最后做的一道,用前面的方法却错误多多,很是烦恼。

2sum中,使用了两种方法,暴力求解(2层for循环)和使用Hashmap的方法;

3sum中,使用2个指针分别指向最大和最小值,同时要查重,避免重复的三元组放入list中。

3Sum Closet中,用两个变量记录当前sum和sum与target间的差距,而无需去重检验,总体思路同3Sum。

4sum中,使用两个for循环和2个指针,分别指向最大和最小的值,然后用HashSet纪录选择过的四元组。

Answer:

public class Solution {private List<List<Integer>> res;public List<List<Integer>> threeSum(int[] nums) {res = new ArrayList<List<Integer>> ();Arrays.sort(nums);for(int i=0; i<=nums.length-3; i++) {if(i!=0 && nums[i] == nums[i-1])continue;deal(nums, i, i+1, nums.length-1);}return res;}public void deal(int[] nums, int i, int p, int q) {while(p<q) {if(nums[p] + nums[q] + nums[i] > 0) {q--;}else if(nums[p] + nums[q] + nums[i] < 0) {p++;}else {List<Integer> li = new ArrayList<Integer> ();li.add(nums[i]);li.add(nums[p]);li.add(nums[q]);res.add(li);p++;q--;while(p<q && nums[p]==nums[p-1]) {p++;}while(p<q && nums[q]== nums[q+1]) {q--;}}}}
}

转载于:https://www.cnblogs.com/little-YTMM/p/4789279.html

LeetCode -- 3Sum相关推荐

  1. Leetcode | 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  2. LeetCode 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  3. LeetCode - 3Sum Closest

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

  4. leetcode 3Sum C++

    荒废好久没更新了,时间过得很快,转眼就2017年了,经历了苦闷的科研阶段,发了论文顺利毕业:也经过三地辗转奔波来去的找工作,最终还是犹犹豫豫选择了自己知道以后可能会后悔的,果然就后悔了.所以还是应该选 ...

  5. LeetCode 3sum 问题

    1. Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  6. 259 [LeetCode] 3Sum Smaller 三数之和较小值

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  7. [LeetCode] 3Sum Closest

    依旧先来题目: Given an array S of n integers, find three integers in S such that the sum is closest to a g ...

  8. [LeetCode] 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  9. [LeetCode]3Sum Closest

    题目 Number: 16 Difficulty: Medium Tags: Array, Two Pointers Given an array S of n integers, find thre ...

最新文章

  1. Vue 环境搭建(win10)
  2. 腾讯AI Lab视觉计算中心招聘计算机视觉算法实习生
  3. esxi所连交换机划vlan导致vm不能通讯
  4. 【Win32汇编】数组求和函数
  5. 菜鸟教程python3 mysql_Python 操作 MySQL 数据库
  6. C#中dynamic、ExpandoObject 的正确用法
  7. Java探索之旅(6)——对象和类
  8. Shell练习题(持续更新)
  9. 使用webpack打包后的vue项目如何运行(express)
  10. java final属性
  11. java server faces
  12. 如何防范电改下分布式光伏的风险
  13. Android保存之SharedPreferences
  14. python泰坦尼克号数据分析_Python实战—泰坦尼克号生还者数据分析
  15. SAP工厂日历的应用
  16. Win7 IE浏览器的降级和升级
  17. HDU2091题解——空心三角形
  18. Annotate点云标注工具
  19. ubuntu14.04搜狗输入法中文无法切换英文
  20. java阅读安卓_基于安卓Android的图书阅读与推荐系统APP的设计

热门文章

  1. 第七章:无模式对话框 和 Windows通用对话框类
  2. 【Python学习系列三】Windows下Python第三方常用库安装
  3. Linux命令ps解读
  4. windows下部署redis
  5. 核心业务系统的内容讨论(管理篇)(转载)
  6. Python学习二——变量和简单数据类型
  7. Python Numpy中返回下标操作函数-节约时间的利器
  8. java linkedlist 方法_Java LinkedList getFirst()方法与示例
  9. python中迭代器的实现原理_Python 进阶应用教程
  10. oracle可视化工具_零代码玩转数据可视化