题目

57. 三数之和

给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。
样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集合的是: (-1, 0, 1) (-1, -1, 2)
注意事项 在三元组(a, b, c),要求a <= b <= c。 结果不能包含重复的三元组。

解析

  • 主要注意去除重复元素的方法
class Solution_57 {
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> &numbers) {
// write your code here
vector<vector<int>> ret;
if (numbers.size() < 3)
return ret;
sort(numbers.begin(), numbers.end());
vector<int> vec;
for (int i = 0; i < numbers.size() - 2; i++)
{
if (i >= 1 && numbers[i - 1] == numbers[i]) //去重复元素
{
continue;
} int b = i + 1;
int c = numbers.size() - 1; while (b<c)
{
while (b<c&&c<numbers.size() - 1 && numbers[c + 1] == numbers[c]) //去重只需要在查找到过后进行,所以放在下面else里面,容易理解一些
c--;
while (b<c&&b - 1>i&&numbers[b - 1] == numbers[b])
b++;
if (b == c)
continue; int sum = numbers[i] + numbers[b] + numbers[c];
if (sum>0)
{
c--;
}
else if (sum < 0)
{
b++;
}
else
{
vec.clear();
vec.push_back(numbers[i]);
vec.push_back(numbers[b]);
vec.push_back(numbers[c]);
ret.push_back(vec);
//break; //bug
c--;
b++;
}
}
}
return ret;
} vector<vector<int>> threeSum(vector<int> &numbers) {
// write your code here
vector<vector<int>> ret;
if (numbers.size() < 3)
return ret;
sort(numbers.begin(), numbers.end());
vector<int> vec;
for (int i = 0; i < numbers.size() - 2; i++)
{
if (i >= 1 && numbers[i - 1] == numbers[i]) //去重复元素
{
continue;
} int b = i + 1;
int c = numbers.size() - 1; while (b<c)
{ int sum = numbers[i] + numbers[b] + numbers[c];
if (sum>0)
{
c--;
}
else if (sum < 0)
{
b++;
}
else
{
vec.clear();
vec.push_back(numbers[i]);
vec.push_back(numbers[b]);
vec.push_back(numbers[c]);
ret.push_back(vec);
//break; //bug
c--;
b++; while (b < c&&numbers[c + 1] == numbers[c])
c--;
while (b < c&&numbers[b - 1] == numbers[b])
b++; }
}
}
return ret;
}
};

57. 三数之和 &&的更多相关文章

  1. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

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

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

  4. lintcode: 三数之和II

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

  5. lintcode:三数之和

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

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

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

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

  8. LeetCode第十六题-找出数组中三数之和最接近目标值的答案

    3Sum Closest 问题简介: 给定n个整数的数组nums和整数目标,在nums中找到三个整数,使得总和最接近目标,返回三个整数的总和,可以假设每个输入都只有一个解决方案 举例: 给定数组:nu ...

  9. 南大算法设计与分析课程OJ答案代码(4)--变位词、三数之和

    问题 A: 变位词 时间限制: 2 Sec  内存限制: 10 MB提交: 322  解决: 59提交 状态 算法问答 题目描述 请大家在做oj题之前,仔细阅读关于抄袭的说明http://www.bi ...

随机推荐

  1. Plant Simulation常用命令

    Plant Simulation 是面向对象的三维离散事件仿真软件,使您能够快速.直观地构建逼真的物流模型. 您还可以使用高级统计工具执行复杂的生产分析.以下介绍该软件的一些常用命令. 1. clea ...

  2. BZOJ.4816.[SDOI2017]数字表格(莫比乌斯反演)

    题目链接 总感觉博客园的\(Markdown\)很..\(gouzhi\),可以看这的. 这个好像简单些啊,只要不犯sb错误 [Update] 真的算反演中比较裸的题了... \(Descriptio ...

  3. .NET开源了,Visual Studio开始支持 Android 和 iOS 编程并自带Android模拟器

    北京时间今天凌晨的大会上,多少程序员的假想成为现实..NET 开源,集成 Clang 和 LLVM 并且自带 Android 模拟器,这意味着 Visual Studio 这个当下最好没有之一的 ID ...

  4. LPC43xx Asymmetric Dual Core : Cortex-M0 and Cortex-M4

  5. [Go] 命令行参数解析包(flag 包)使用详解

    Go 的 flag 包可以解析命令行的参数. 一.命令行语法 命令行语法主要有以下几种形式: cmd -flag       // 只支持bool类型 cmd -flag=xxx cmd -flag ...

  6. ProFTPd Local pr_ctrls_connect Vulnerability - ftpdctl 漏洞及攻击代码分析

    攻击代码网址:http://www.exploit-db.com/exploits/394/ 1.执行环境: 1.ProFTPD 1.3.0/1.3.0a 2.编译ProFTPD时.--enable- ...

  7. 阅读Linux内核源码时建立tags索引

    比如在阅读arm架构的Linux内核代码时想建立arm架构相关的索引,可以使用下面的命令: make ARCH=arm tags

  8. Android 实现页面跳转并传递参数教程

    首先我们来看一下实现的功能:     第二,我们看一下实现这个功能,总共会接触到哪些文件和代码. 1.实现本功能总共涉及如下6个文件 2.实现本功能,总共涉及如下6个文件中的如下代码: (1) 效果: ...

  9. 【python】python安装tensorflow报错:python No matching distribution found for tensorflow==1.12.0

    python安装tensorflow报错:python No matching distribution found for tensorflow==1.12.0 python版本是3.7.2 要安装 ...

  10. Android Studio 出现 Gradle's dependency cache may be corrupt 错误分析

    http://blog.csdn.net/u014231734/article/details/41913775 情况说明: 之前下载了 Android Studio 1.0rc2候选版,那时候把 S ...