题意:给一个vector<int>容器,要求每当找到3个元素之和为0时就将这3个数按大小顺序记下来,用一个二维vector返回。也就是vector< vector<int> >类型的。

思路:2sum是用的两个指针,那么3sum就可以利用2sum的思路解决,假如先挑一个元素出来,则还需挑2个元素,就可以用2sum的思路了。首先将n个元素排序先。怎么挑第1个元素?一共有n种选择,但是为了防止重复,后2个不用挑,留给两个指针来解决,也就是有n-2个元素。而两个指针的可以挑的有哪些呢?假设第1个元素是第i个,那么这两个指针应该在第i个之后的元素挑,为什么呢?

比如数组为:-2 -1 0 1 2

第一轮循环:

  第一个元素:-2

  第二个元素:0

  第三个元素:2

第2轮循环应该是:

  第一个元素:-1

  第二个元素:0  (难道还想试试-2? -2可以搭配的所有可能都已经试出来了,这里不用再重试了)

  第三个元素:1

 class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
sort(num.begin(), num.end()); //排序
vector<int> group(,);
vector< vector<int> > ans;
int n=num.size(),sum,sum2,*pl,*pr,old=;
for(int i=; i<n-; i++ )
{
if( num[i] == old ) continue;//去重,首数字不能重复
else old = num[i];
sum2 = - num[i];//寻找余下两数之和
pl = &num[i+];//左指针
pr = &num[n-];//右指针
while(pl!=pr)
{
sum = *pl + *pr;
if( sum == sum2 )
{
if( group[]!=num[i] || group[]!=*pl || group[]!=*pr)//去重,防止找到的和上一次刚好一样
{
group[] = num[i];
group[] = *pl;
group[] = *pr;
ans.push_back(group);
}
pl++;
}
else if( sum > sum2 ) pr--;
else pl++;
}
}
return ans;
}
};

3Sum

LeetCode OJ 3Sum 3个整数之和的更多相关文章

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

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

  3. LeetCode OJ:Combination Sum (组合之和)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  4. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  5. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  6. leetcode python两整数之和

    # Leetcode 371 两整数之和***### 题目描述 **不使用**运算符 `+` 和 `-` ​​​​​​​,计算两整数 `​​​​​​​a `.`b` ​​​​​​​之和. **示例1: ...

  7. Java实现 LeetCode 371 两整数之和

    371. 两整数之和 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a .b ​​​​​​​之和. 示例 1: 输入: a = 1, b = 2 输出: 3 示例 2: 输入: ...

  8. Leetcode OJ 刷题

    Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...

  9. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

随机推荐

  1. 关闭QtCreator的vim风格编辑模式

    今天不小心点到了键盘的快捷键Alt+V,使QtCreator进入了vim风格编辑模式,导致快捷键拷贝粘贴都不正常,找了下资料才发现是这个问题.具体操作如下: 打开QtCreator去掉下列位置的勾选或 ...

  2. redis系列:RDB持久化与AOF持久化

    前言 什么是持久化? 持久化(Persistence),即把数据(如内存中的对象)保存到可永久保存的存储设备中(如磁盘).持久化的主要应用是将内存中的对象存储在数据库中,或者存储在磁盘文件中.XML数 ...

  3. 基于TMF SID的高可扩展性数据模型

    基于TMF SID的高可扩展性数据模型 前言 此文根据TMF SID规范撰写,欢迎大家提出建议和意见. TMF文档版权信息 Copyright © TeleManagement Forum 2013. ...

  4. C# 外观模式

    外观模式(Facade Pattern) 介绍 定义:  为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.使用外观模式时,创建了一个统一的类,用来 ...

  5. 交叉编译Spice-gtk

    Fedora环境 编译环境 操作系统: 64位 Fedora23 下载源文件 spice-gtk.spice-protocol 安装依赖 $ sudo yum install -y dh-autore ...

  6. validate验证注册表单

    点击预览; <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...

  7. git分支主干

    ~/Desktop/work/movies/movie(apps) $ git status  //先查看是否有需要提交的东西# On branch appsnothing to commit (wo ...

  8. 学习:数学----gcd及扩展gcd

    gcd及扩展gcd可以用来求两个数的最大公因数,扩展gcd甚至可以用来求一次不定方程ax+by=c的解   辗转相除法与gcd 假设有两个数a与b,现在要求a与b的最大公因数,我们可以设 a=b*q+ ...

  9. Webpack热加载和React(其中有关于include和exclude的路径问题)

    看了几个React配合webpack的教程,大部分都因为版本问题过时了.终于找到了一个不错的教程.记录下其中的知识点. 首先万分感谢这个教程的制作者.少走了许多弯路,正在学习webpack的小伙伴可以 ...

  10. windows下安装python包

    1.windows下成功安装好python后,在安装目录的Scripts目录下有easy_install和pip工具 2.如果没有安装pip,进入命令行,切换到python的安装目录下的Scripts ...