LeetCode -- 3Sum
Question:
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.
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相关推荐
- 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 ...
- 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 ...
- LeetCode - 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 C++
荒废好久没更新了,时间过得很快,转眼就2017年了,经历了苦闷的科研阶段,发了论文顺利毕业:也经过三地辗转奔波来去的找工作,最终还是犹犹豫豫选择了自己知道以后可能会后悔的,果然就后悔了.所以还是应该选 ...
- 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 ...
- 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 ...
- [LeetCode] 3Sum Closest
依旧先来题目: Given an array S of n integers, find three integers in S such that the sum is closest to a g ...
- [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 ...
- [LeetCode]3Sum Closest
题目 Number: 16 Difficulty: Medium Tags: Array, Two Pointers Given an array S of n integers, find thre ...
最新文章
- Vue 环境搭建(win10)
- 腾讯AI Lab视觉计算中心招聘计算机视觉算法实习生
- esxi所连交换机划vlan导致vm不能通讯
- 【Win32汇编】数组求和函数
- 菜鸟教程python3 mysql_Python 操作 MySQL 数据库
- C#中dynamic、ExpandoObject 的正确用法
- Java探索之旅(6)——对象和类
- Shell练习题(持续更新)
- 使用webpack打包后的vue项目如何运行(express)
- java final属性
- java server faces
- 如何防范电改下分布式光伏的风险
- Android保存之SharedPreferences
- python泰坦尼克号数据分析_Python实战—泰坦尼克号生还者数据分析
- SAP工厂日历的应用
- Win7 IE浏览器的降级和升级
- HDU2091题解——空心三角形
- Annotate点云标注工具
- ubuntu14.04搜狗输入法中文无法切换英文
- java阅读安卓_基于安卓Android的图书阅读与推荐系统APP的设计