http://www.itint5.com/oj/#20

其实是3sum的变种,有重复数字,但是一开始还是写错了。其实是选定一个后,在右边剩余数组里找2sum,找到一组后继续找。

#include <algorithm>
using namespace std;
typedef tuple<int, int, int> ABC; //存放a,b,c三元组 //返回所有满足条件的(a,b,c)三元组
vector<ABC> threeSumZero(vector<int> &arr) {
vector<ABC> ans;
sort(arr.begin(), arr.end());
int k = 0;
while (k < arr.size()) {
int i = k+1;
int j = arr.size() - 1;
while (i < j) {
if (arr[i] + arr[j] == -arr[k]) {
ans.push_back(make_tuple(arr[k], arr[i], arr[j]));
while (i+1 < arr.size() && arr[i] == arr[i+1]) i++;
while (j-1 >= 0 && arr[j-1] == arr[j]) j++;
i++;
j--;
} else if (arr[i] + arr[j] < -arr[k]) {
i++;
} else {
j--;
}
}
while (k+1 < arr.size() && arr[k] == arr[k+1]) k++;
k++;
}
return ans;
}

  

[itint5]三数和为0的更多相关文章

  1. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  2. 3sum 求三数之和等于0,不允许重复

    https://leetcode.com/problems/3sum/ 套路比较常见了,最重要的是去重.还是没法一次通过. class Solution { public: vector<vec ...

  3. LeetCode第十五题-找出数组中三数和为0的答案

    3Sum 问题简介: 给定n个整数的数组nums,是否有元素a,b,c在nums中,使a + b + c = 0? 找到数组中所有唯一的三元组,它们的总和为零 注:解决方案集不得包含重复的三元组 例如 ...

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

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

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

  6. 2. 三数之和(数组、hashset)

    思路及算法: 该题与第一题的"两数之和"相似,三数之和为0,不就是两数之和为第三个数的相反数吗?因为不能重复,所以,首先进行了一遍排序:其次,在枚举的时候判断了本次的第三个数的值是 ...

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

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

  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. lintcode: 三数之和II

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

随机推荐

  1. ajax — get? or post?

    ajax - get? or post? 与 POST 相比,GET 更简单也更快,并且在大部分情况下都能用. 然而,在以下情况中,请使用 POST 请求: 无法使用缓存文件(更新服务器上的文件或数据 ...

  2. VS2013编译WEBKIT

    0,安装VS2013:DXSDK_Jun10.exe:QuickTimeSDK.exe 1,WebKit-r174650.tar.bz2 以管理员解压(非管理员解压最后几下总是报错) 2,设置环境变量 ...

  3. 【转帖】error C2296: “^”: 非法,左操作数包含“double”类型

    想要实现 ,写的C++程序为 double x; x=2^3; 结果程序总是出现这样的错误:error C2296: “^”: 非法,左操作数包含“double”类型 后来才发现操作符“^”,在C++ ...

  4. Xamarin android PreferenceActivity 实现应用程序首选项设置(一)

    应用程序首选项屏幕 类似系统设置界面. PreferenceActivity 是另一种类型的Activity,通过PreferenceActivity 可以以最少量的工作显示某些Preference列 ...

  5. DTCMS通用分页列表

    <%set DataTable newsList=get_article_list(channel, category_id, pagesize, page, "status=0&qu ...

  6. spark 1.3.0下的问题

    1.在spark SQL的一个test中 无论是registerAsTable还是registerTempTable 都会有问题,经过查找各种资料,采用如下的方式: val sqlCon=new or ...

  7. sirius的python学习笔记(1)

    1.可以通过try...except语句来简单的判断字符串是否为整数值,如例程 x = raw_input('>') try: print int(x) except ValueError: r ...

  8. cmd&Linux 下使用mysql全记录

    php mysql数据库常用cmd命令集 show databases; 显示数据库 create database name; 创建数据库 use databasename; 选择数据库 drop ...

  9. EXPLAIN句法 优化表结构

    EXPLAIN tbl_name or EXPLAIN SELECT select_options EXPLAIN tbl_name是DESC[RIBE] tbl_name或SHOW COLUMNS ...

  10. iframe 刷新

    iframe刷新父页面 parent.location.reload(); iframe 一个子页面操作过后,刷新指定子页面 parent.frames('ifrmname').location.re ...