lintcode-57-三数之和
57-三数之和
给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。
注意事项
在三元组(a, b, c),要求a <= b <= c。
结果不能包含重复的三元组。样例
如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集合的是:
(-1, 0, 1)
(-1, -1, 2)标签
数组 排序 两根指针 脸书
思路
参考资料
首先升序排序数组
然后定义下标变量 k , i , j 。如果简单的遍历,那么跟是否有序没有关系,其时间复杂度将达到O(n^3)。
但是,如果当前选择了a、b、c三个数,如果其和小于目标target,那么需要将其中一个数用更大的数替换;反之亦然。但究竟替换三个数中的哪个数?可以先固定一个数(k),当前值小于target时,可以让 i 增加;否则,j 减小。
code
class Solution {
public:
/**
* @param numbers : Give an array numbers of n integer
* @return : Find all unique triplets in the array which gives the sum of zero.
*/
vector<vector<int> > threeSum(vector<int> &nums) {
// write your code here
int size = nums.size(), target = 0;
if(size < 3) {
return vector<vector<int> >();
}
vector<vector<int> > result;
int i = 0, j = 0, k = 0;
sort(nums.begin(), nums.end());
for(k=0; k<size; k++) {
if(k>0 && nums[k]==nums[k-1]) {
continue;
}
for(i=k+1, j=size-1; i<j;) {
if(i>k+1 && nums[i]==nums[i-1]) {
i++;
continue;
}
if(j<size-1 && nums[j]==nums[j+1]) {
j--;
continue;
}
int sum = nums[i] + nums[j] + nums[k];
if(target == sum) {
vector<int> temp;
temp.push_back(nums[k]);
temp.push_back(nums[i]);
temp.push_back(nums[j]);
result.push_back(temp);
i++;
j--;
}
else if(target < sum) {
j--;
}
else {
i++;
}
}
}
return result;
}
};
lintcode-57-三数之和的更多相关文章
- 57. 三数之和 &&
题目 57. 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的 ...
- lintcode:三数之和
题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...
- [Lintcode 3sum]三数之和(python,二分)
题目链接:http://www.lintcode.com/zh-cn/problem/3sum/?rand=true# 用这个OJ练练python…这个题意和解法就不多说了,O(n^2lgn)就行了, ...
- [LintCode] 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 ...
- lintcode: 三数之和II
题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = . 和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 ...
- [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 given num ...
- [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 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 ...
- LeeCode数组第15题三数之和
题目:三数之和 内容: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...
随机推荐
- 插入排序_C语言_数组
插入排序_C语言_数组 #include <stdio.h> void insertSort(int *); int main(int argc, const char * argv[]) ...
- ssm整合实现注册与登录功能
最简洁易懂的SSM整合源码都在这里了 激情提示: 1.本项目是用IDEA编写的,不管你是习惯何种ide工具,那也只是工具而已,源代码才是本质 2.本项目只拥有注册和登录功能,简易的功能和详细的注释,是 ...
- 蒜头君学英语--set()练习
题目描述 蒜头君快要考托福了,这几天,蒜头君每天早上都起来记英语单词.花椰妹时不时地来考一考蒜头君:花椰妹会询问蒜头君一个单词,如果蒜头君背过这个单词,蒜头君会告诉花椰妹这个单词的意思,不然蒜头君会跟 ...
- (转)阿里巴巴Druid数据源及使用
原文链接:https://blog.csdn.net/yanguo110/article/details/68944659 第一部分:数据源的集中比较. 目前常用的数据源主要有c3p0.dbcp.pr ...
- C# 多条件拼接sql
#region 多条件搜索时,使用List集合来拼接条件(拼接Sql) StringBuilder sql = new StringBuilder("select * from PhoneN ...
- 文件 I/O缓冲流
import java.io.File; import java.io.Writer; import java.util.StringTokenizer; import java.io.Reader; ...
- POJ1659 可图性判定
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10660 Accepted: 4 ...
- centos 安装java1.8
https://www.cnblogs.com/xuliangxing/p/7066913.html
- HDU暑假多校第三场H.Monster Hunter
一.题意 给定一个树状地图,每个树节点上有一只怪物,打死一只怪物的过程中将会消耗A点HP,打死之后将会获得B点HP.因为树状结构,所以每只怪物必须先打死父节点的怪兽之后在打死子节点的怪物.现在,给定每 ...
- LeetCode:12. Integer to Roman(Medium)
1. 原题链接 https://leetcode.com/problems/integer-to-roman/description/ 2. 题目要求 (1) 将整数转换成罗马数字: (2) 整数的范 ...