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. 基于 HTML5 的数据存储

    以前想做个静态网页APP.最初的思路是用本地文件存储数据,后来发现在手机上运行时,文件无法找到. 经过了长达几个月的搜索(实际也就几天),没有找到合适的方法. 就在绝望的时候,无意间搜到基于HTML5 ...

  2. JavaScript的语法规则

    JavaScript的语法规则 JavaScript区分大小写 JavaScript脚本程序须嵌入在HTML文件中 JavaScript脚本程序中不能包含HTML标记代码 每行写一条脚本语句 语句末尾 ...

  3. SQLite学习心得

    SQLite是一款很有名气的小型开源跨平台数据库,作为目前最流行的开源嵌入式关系型数据库,在系统结构设计中正在扮演着越来越重要的角色. 本文主要沿着 http://www.cppblog.com/we ...

  4. Spring零碎知识复习

    自学了Spring也有一段时间了,多多少少掌握了一些Spring的知识,现在手上也没有很多的项目练手,就将就着把这些学到的东西先收集起来,方便日后用到的时候没地方找. 1.spring的国际化 主要是 ...

  5. 【实习记】2014-08-19升级vim配置YouCompleteMe并debug的过程+qtcreator有语言包没法换语言

        做了个小项目,有空闲可以做点事了. 偶然查资料看见YouCompleteMe的鼎鼎大名. 演示demo <img src="http://i.imgur.com/0OP4ood ...

  6. c#怎么获取当前页面的url

    Request.ApplicationPath: /testwebRequest.CurrentExecutionFilePath: /testweb/default.aspxRequest.File ...

  7. DataGridView显示时间格式

    默认显示时间不显示秒yyyy-MM-dd HH:mm dataGridView.Columns["日期时间字段"].DefaultCellStyle.Format = " ...

  8. 一个例子说明如何在DataSnap中使用FireDAC

    一.FireDAC调用DataSnap远程方法查询数据示例 1.服务端使用FDQUERY查询数据并返回TDATASET: function TServerMethods1.GetData(var sq ...

  9. Spark Streaming揭秘 Day24 Transformation和action图解

    Spark Streaming揭秘 Day24 Transformation和action图解 今天我们进入SparkStreaming的数据处理,谈一下两个重要的操作Transfromation和a ...

  10. wpf datagrid 行双击事件

    Xaml: <DataGrid ItemsSource="{Binding SessionList}" Grid.Row="2" Grid.Column= ...