LeetCode OJ:Three Sum(三数之和)
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)
大体的思想先将数组排序,从小到大取vector中的数first,再从剩下的数中取和等于 0 - first 的数即可。下面是代码(一开始没想出来,然后参考了别人的解法在写出来,一般的三层循环谁都能想到,但是时间复杂度太高,这里的这个时间复杂度应该是O(N^2),还是可以接受的)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
vector<vector<int>> result;
int sz = nums.size();
sort(nums.begin(), nums.end());
for (int i = ; i < sz - ; ++i){
twoSum(nums, i + , - nums[i], result);
while(nums[i] == nums[i + ]) ++i;//这一步要注意,防止得出重复的vector
}
return result;
}
void twoSum(vector<int> & nums, int start, int value, vector<vector<int>> & ret)
{
int beg = start;
int end = nums.size()-;
while (beg < end){
int sum = nums[beg] + nums[end];
if (sum < value)
beg++;
else if (sum > value)
end--;
else{
ret.push_back(vector<int>{nums[start - ], nums[beg], nums[end]});
while (nums[beg + ] == nums[beg]) beg++;//这一步的处理应该注意,防止出现相同的vector
while (nums[end - ] == nums[end]) end--;
beg++, end--;
}
}
}
};
java版的代码如下所示:
(由于不太熟悉ArrayList和List之间的关系,写起来感觉各种坑爹啊,注意下List和ArrayList之间的各种转换就可以了,代码如下):
public class Solution {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
for(int i = 0; i < nums.length - 2; ++i){
twoSum(nums, i+1, 0 - nums[i]);
while(i < nums.length - 2 && nums[i] == nums[i+1])
++i;
}
return ret;
}
public void twoSum(int[] nums, int start, int value)
{
int beg = start;
int end = nums.length - 1;
while(beg < end){
if(nums[beg] + nums[end] == value){
List<Integer> list = new ArrayList<Integer>();
list.add(nums[start - 1]);
list.add(nums[beg]);
list.add(nums[end]);
ret.add(list);
while(beg < end && nums[beg+1] == nums[beg])
beg++;
while(beg < end && nums[end-1] == nums[end])
end--;
beg++;
end--;
}else if(nums[beg] + nums[end] > value){
end--;
}else{
beg++;
}
}
}
}
PS:同样的4Sum问题也可以转换成上面的3Sum问题,从而递归的求解,KSum问题也是一样
LeetCode OJ:Three Sum(三数之和)的更多相关文章
- LeetCode#15 | Three Sum 三数之和
一.题目 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意:答案中不可以包含 ...
- [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】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- [LeetCode] 259. 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- LeetCode 第15题-三数之和
1. 题目 2.题目分析与思路 3.思路 1. 题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且 ...
- LeetCode 15. 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] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- [LeetCode]1.Two Sum 两数之和&&第一次刷题感想
---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...
- [leetcode]1. Two Sum两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific t ...
随机推荐
- oracle入门(2)—— 使用图形工具navicat for oracle
[本文介绍] 本文将介绍如何使用图形工具navicat for oracle连接本地数据库 以及远程访问 服务器数据库. [下载地址] http://www.navicat.com.cn/downlo ...
- 维多利亚的秘密 golang入坑系列
原文在gitbook,字字原创,版权没有,转载随意. 在写本文的前一天,2017维密在上海开始了. 为了纪念屌丝界的盛世,特为本节起名维多利亚的秘密.现在的社会,要想出名只有抓眼球.所以写份技术文章, ...
- HTTP状态码集
1xx消息 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非在某些试 ...
- Spring 之定义切面尝试(基于注解)
[Spring 之定义切面尝试] 1.标记为深红色的依赖包是必须的 <dependency> <groupId>org.springframework</groupId& ...
- php操作redis cluster集群成功实例
java操作redis cluster集群可使用jredis php要操作redis cluster集群有两种方式: 1.使用phpredis扩展,这是个c扩展,性能更高,但是phpredis2.x扩 ...
- 获取微信公众号用户的基本信息(UnionID机制)
获取用户基本信息(UnionID机制) 在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的.对于不同公众号,同一用户的open ...
- Spring Boot 中配置切换
步骤一:切换需求 有时候在本地测试是使用8080端口(默认端口),可是上线时使用的比如是9090端口(不常用的,以防被黑). 此时就可以通过多配置文件实现多配置支持与灵活切换. 步骤二:多配置文件 3 ...
- spark启动
注意在启动spark时候要指定参数 要不就死启动的单机版的 /usr/local/spark-1.5.2-bin-hadoop2.6/bin/spark-shell \ --master spark: ...
- Shell awk 求标准差
cat > temp000180255798957892187719 awk '{x[NR]=$0; s+=$0; n++} END{a=s/n; for (i in x){ss += (x[i ...
- [BZOJ1116]CLO[并查集]
看了样例突然发现= =无向边不会增加入度. 然后发现是环套环. 一个环所有点入度都为2. 最后的图无视所有无向边的话大概是这样的(将就一下 然后就可以并查集维护一下联通性... 当x , y属于一个联 ...