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. 【转】Java学习---垃圾回收算法与 JVM 垃圾回收器综述

    [原文]https://www.toutiao.com/i6593931841462338062/ 垃圾回收算法与 JVM 垃圾回收器综述 我们常说的垃圾回收算法可以分为两部分:对象的查找算法与真正的 ...

  2. 【转】Nginx学习---负载均衡的原理、分类、实现架构,以及使用场景

    [原文]https://www.toutiao.com/i6593604356799463944/ [原文]https://www.toutiao.com/i6592741060194075143/ ...

  3. JAVA 连接 SQL Server 2008:java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

    新项目需要修改Java开发的MES系统...Java忘的也差不多了...简单尝试以下JAVA连接SQL Server吧,没想到坑还是很多的.以前直接连oracle时没有这么多麻烦啊....可能微软和o ...

  4. NOIP2018考前抱佛脚——图论基础复习

    目录 存图方式 邻接矩阵存图 邻接表存图 链式前向星 最小生成树 例1 P1536 村村通 题目描述 输入输出格式 输入输出样例 标程 例2 P1546 最短网络 Agri-Net 题目背景 题目描述 ...

  5. 一篇关于介绍php的几个user 认证相关的几个包

    http://kodeinfo.com/post/laravel-authentication-packages LARAVEL AUTHENTICATION PACKAGES By Imran Iq ...

  6. BZOJ 1834 网络扩容 最大流+最小费用流

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1834 题目大意: 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是 ...

  7. python第二十六课——装饰器

    装饰器是闭包的一种使用场景: python中的装饰器在定义上需要传入一个函数对象, 在此函数执行之前或者之后都可以追加其它的操作, 这样做的好处是,在不改变源码(原本业务逻辑的)同时,进行功能的扩展: ...

  8. linux配置路径PATH问题

    临时:           终端输入          export PATH=/myPath:$PATH  等号左右无空格   永久:           在用户家目录下即-目录,         ...

  9. 天地图api地址

    天地图地址 http://lbs.tianditu.com/api-new/examples.html 参考资料 http://lbs.tianditu.com/api-new/class.html

  10. linux 的常用命令---------第二阶段

    vim编辑器 vim 文件名(首先进入命令模式) :(进行编辑文件内容)  → 按 i 键进入插入模式,可以写内容啦. ↓ 按 Esc 键,进入命令模式 ↓ 按 shift + : 键,进入末行模式  ...