Given an array nums of n integers, are there elements abc in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
(4Sum问题)
C++:
 class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
if (nums.size() <= )return result;
sort(nums.begin(), nums.end());
for (int i = ; i < nums.size() - ; i++) {
int a = nums[i];
if (a > ) break;
if (i > && a == nums[i - ]) continue;
for (long j = i + , k = nums.size() - ; j < k;) {
int b = nums[j];
int c = nums[k];
int value = a + b + c;
if (value == ) {
result.push_back(vector<int>({ a, b, c }));
while (b == nums[++j] && j<k);
while (c == nums[--k] && j<k);
}
else if (value > ) {
k--;
}
else {
j++;
}
}
}
return result;
}
};

3Sum(or k_Sum)的更多相关文章

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

  2. 3Sum algorithm - 非常容易理解的实现 (java)

    原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...

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

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

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

  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. 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. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  8. 16. 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

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

随机推荐

  1. python-ldap修改AD域用户密码(CA+SSL)

    代码连接:https://github.com/raykuan/ldap-notes 使用python的ldap模块连接AD服务器,有两种方式: 非加密:con = ldap.initialize(' ...

  2. tp5 migrate数据库迁移工具

    tp5相对与tp3.2有很大的不同 migrate是其中一点,通过migrate程序员可以在php代码中创建数据库修改回滚等操作 首先下载migrate扩展,命令行到当前项目目录下执行 compose ...

  3. DFS服务待书写

    https://www.cnblogs.com/xfan1982/p/4120583.html 安装AD域控制 https://www.cnblogs.com/wanggege/p/4605678.h ...

  4. 粗略的整改一下blog

    一.先找个简约的模板:看个人喜好咯 二.页面定制CSS: 1.首先,查看主页源码,了解一下各个标签的id,引用的class等 2.通过操作相应的id,class,和标签,进行个性化.这里需要具备看懂和 ...

  5. 【Alpha 冲刺】 10/12

    今日任务总结 人员 今日原定任务 完成情况 遇到问题 贡献值 胡武成 完成app端api编写 未完成 Json格式出了点问题,修复中 孙浩楷 图片在线编辑器插件引入 未完成 耦合了,结果另外一个那边做 ...

  6. 翻译:使用红外传感器与Arduino进行简单动作与手势检测

    译注:昨天看 Adruino 的 Twitter 推了这篇项目,第一眼就觉得非常有趣,翻译给大家看看.文中的红外传感器比较高级,和淘宝上5块钱的那种只能输出0和1的不一样, TPA81 是可以输出温度 ...

  7. windows下的MySql实现读写分离

    MySql读写分离 1.删除系统服务 sc delete 服务名 2.复制安装好的3380文件夹到3381 3.进入3381\logs目录下将所有文件删除 4.进入3381\data目录,将所有的lo ...

  8. HTML5中的Canvas详解

    什么是Canvas HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图形容器,您必须使用脚本来绘制图 ...

  9. SQL必知必会摘要

    数据检索 2.2 检索单个列 SELECT prod_name FROM Products; SQL语句不区分大小写   2.3 检索多个列 SELECT prod_name,prod_id,prod ...

  10. velocity模板实战

    场景:json配置报文转换遇到的问题:1.json报文转换成map,多节点如何处理?数组如何处理? 2.velocity模板处理数组 3.应用之间rabbitmq通讯map反序列化,数组报错?知识点: ...