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-三数之和的更多相关文章

  1. 57. 三数之和 &&

    题目 57. 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的 ...

  2. lintcode:三数之和

    题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...

  3. [Lintcode 3sum]三数之和(python,二分)

    题目链接:http://www.lintcode.com/zh-cn/problem/3sum/?rand=true# 用这个OJ练练python…这个题意和解法就不多说了,O(n^2lgn)就行了, ...

  4. [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 ...

  5. lintcode: 三数之和II

    题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = .  和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 ...

  6. [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 given num ...

  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 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 ...

  10. LeeCode数组第15题三数之和

    题目:三数之和 内容: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...

随机推荐

  1. Struts2后期(这框架目前正处于淘汰状态)

    Struts2第三天 课程回顾:Struts2框架的第二天 1. Servlet的API * ActionContext对象 * ServletActionContext对象 2. 结构类型的跳转 * ...

  2. iOS Block界面反向传值小demo

    1.在第二个视图控制器的.h文件中定义声明Block属性: // 定义block @property (nonatomic, copy) void (^NextViewControllerBlock) ...

  3. window.location.href url含中文乱码问题

    (1).页面中先对中文进行编码. 如:window.location.href = url+"&groupName=" + encodeURI(encodeURI(grou ...

  4. 一、spring 4概述

    0 前言 0.0 Spring 来历 Spring 是于2003年兴起的一个轻量级的Java 开发框架, 为了解决企业应用开发的复杂性而创建, 核心是控制反转(IoC)和面向切面编程(AOP). 简单 ...

  5. Sql Server 查看存储过程最后修改时间

    Sql Server 查看存储过程最后修改时间 select * from sys.procedures order by modify_date desc

  6. JS数组push一个对象

    这个是正确的数据添加对象 var dypieArr = []; var dyArr = []; var arrStr = ''; for(var i = 0; i < dataStreet.le ...

  7. 【mysql处理远程登陆授权及数据库迁移备份问题】

    Database changedMariaDB [mysql]> grant all PRIVILEGES on mysql.* to root@'%' identified by '123'; ...

  8. H5混合开发进阶

    混合开发: 原生app里面,IOS 安卓的原生app里面,嵌套h5界面. 通过原生app里的一个webView盒子进行交互.webView是原生app内置的一个XXX,里面可以放置h5界面.可以相互调 ...

  9. mysql在cmd里中文乱码解决办法

    右边画红线部分中文已经乱码,左边红线里中文则完美显示出来了. 解决办法   用set  names   utf-8: 效果如图

  10. 用pathon实现计算器功能

    实现计算类似公式的计算器程序1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3* ...