题目

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. ARP欺骗防御工具arpon

    ARP欺骗防御工具arpon   ARP欺骗是局域网最为常见的中人间攻击实施方式.Kali Linux提供一款专用防御工具arpon.该工具提供三种防御方式,如静态ARP防御SARPI.动态ARP防御 ...

  2. 膨胀卷积与IDCNN

    Dilation 卷积,也被称为:空洞卷积.膨胀卷积. 一.一般的卷积操作: 首先,可以通过动态图,理解正常卷积的过程: 如上图,可以看到卷积操作. 对于CNN结构,通常包括如下部分: 输入层 (in ...

  3. java 使用grpc步骤

    1.配置grpc maven依赖 <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-ne ...

  4. 关于操作Access数据库jdk选择问题

    关于操作Access数据库,使用jdk64位无法通过ODBC无法获取数据,只能通过jdk32位进行开发.

  5. js跨域请求(jsonp)

    jsonp是跨域请求的手段之一. jsonp的原理: 先来看看下面这段代码 <!DOCTYPE html> <html lang="en"> <hea ...

  6. jQuery.fn.extend()和jQuery.extend()

    jQuery.fn.extend( object ) 一个对象的内容合并到jQuery的原型,以提供新的jQuery实例方法. jQuery.fn.extend()方法继承了jQuery原型($.fn ...

  7. 使用清华源和阿里源替代Ubuntu源

    sudo nano /etc/apt/source.list 替换为如下文本 # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb https://mirrors. ...

  8. spring boot 集成 shiro

    写在前面 1.Shiro是Apache下的一个开源项目,我们称之为Apache Shiro.它是一个很易用与Java项目的的安全框架,提供了认证.授权.加密.会话管理,与spring Security ...

  9. CRC 概述

    Acquired from: ftp.adelaide.edu.au:/pub/rocksoft/crc_v3.txt or ftp://ftp.rocksoft.com/papers/crc_v3. ...

  10. C# 对WinForm应用程序的App.config的使用及加密

    原文地址:http://blog.163.com/zhou_zzq/blog/static/1019622120137621739874/   我们在写C#应用程序时,在工程文件中放置一个app.co ...