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 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)
2、
这道题引申与2sum问题,之后还可以继续引申4sum。
具体的思路就是双指针的灵活应用;
3、我的解法:
1 List<List<Integer>> ret = new ArrayList<List<Integer>>(); 2 public List<List<Integer>> threeSum(int[] num) { 3 4 if (num == null || num.length < 3) return ret; 5 6 Arrays.sort(num); 7 8 int len = num.length; 9 for (int i = 0; i < len-2; i++) { 10 if (i > 0 && num[i] == num[i-1]) continue; 11 find(num, i+1, len-1, num[i]); //寻找两个数与num[i]的和为0 12 } 13 14 return ret; 15 } 16 public void find(int[] num, int begin, int end, int target) { 17 int l = begin, r = end; 18 while (l < r) { 19 if (num[l] + num[r] + target == 0) { 20 List<Integer> ans = new ArrayList<Integer>(); 21 ans.add(target); 22 ans.add(num[l]); 23 ans.add(num[r]); 24 ret.add(ans); //放入结果集中 25 while (l < r && num[l] == num[l+1]) l++; 26 while (l < r && num[r] == num[r-1]) r--; 27 l++; 28 r--; 29 } else if (num[l] + num[r] + target < 0) { 30 l++; 31 } else { 32 r--; 33 } 34 } 35 }
转载于:https://www.cnblogs.com/winterRel/p/5195874.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
Question: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? F ...
- 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年了,经历了苦闷的科研阶段,发了论文顺利毕业:也经过三地辗转奔波来去的找工作,最终还是犹犹豫豫选择了自己知道以后可能会后悔的,果然就后悔了.所以还是应该选 ...
- 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 ...
最新文章
- 兄弟连区块链教程Fabric1.0源代码分析Peer peer根命令入口及加载子命令一
- ubuntu mysql混合开发_mysql5.7主从同步 ubuntu
- ios uiview 如何刷新_2020最新迅雷苹果版如何下载?
- 斐讯k2路由器刷华硕固件做桥接中继
- 北京已开通5G基站6324个,年内目标建设超1万个5G基站
- 企业级WEB应用服务器TOMCAT
- win7设置防火墙允许Ping与telnet
- 检索 COM 类工厂中 CLSID 为{...} 的组件时失败,出现以下错误: 80070005、8000401A的错误解决方法
- 介绍Mybatis与使用(什么是Mybatis?)
- 百变红茶的10种搭配喝法
- 没有权限cd linux,为什么linux在root权限下 打cd命令没有用,都显示没有那个目录或文件...
- IDEA3.5最新版激活码
- python selenium 处理弹窗_Selenium自动化发布话题,以及获取window.showModalDialog()弹窗对象(Python实现)...
- oracle 所有外键约束,如何在oracle中找到所有外键约束?
- Spring Security(1) 入门体验
- 简单的项目规模统计方法
- linux apache http403,解决apache下http请求403 Forbidden错误
- windows进程间通讯-邮路
- 思行合一——努力提升自己
- 工资管理系统(纯C)
热门文章
- 摄像头成像数据可见窗口计算
- 安装工程造价课程设计_安装工程造价课程设计的图纸-上海装修报价
- 透明怎么弄_玻璃球里的花纹是怎么弄进去的?谜终于解开了!
- Oracle中并行parallel用法
- java比较吊的程序代码_java中 compareTo()的程序代码及用法
- 乐玩自动化测试模块_五大测试框架介绍,附带全套黑马自动化测试视频教程(完结)...
- 95-25-010-命令-topic 创建过程
- 【Elasticsearch】Elasticsearch 搜索体验可量化的指标 查准率(精确率)、查全率(召回率)
- 【clickhouse】docker安装clickhouse后本地无法连接
- IDEA 安装插件Plugin 'Scala' is incompatible with this installation