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. spark-机器学习实践-K近邻应用实践一

    K近邻应用-异常检测应用 原理: 根据数据样本进行KMeans机器学习模型的建立,获取簇心点,以簇为单位,离簇心最远的第五个点的距离为阈值,大于这个值的为异常点,即获得数据异常. 如图:

  2. 团队作业——Alpha冲刺 1/12

    团队作业--Alpha冲刺 Alpha 阶段认领的任务 杨光海天:加入随心摘首页和编辑界面的开发中,并完成冲刺博文的撰写 郭剑南.周琪文:图像识别核心算法的实现 赖志平:随心摘首页和编辑界面开发主力, ...

  3. PyQt5--QFileDiaglog

    # -*- coding:utf-8 -*- ''' Created on Sep 17, 2018 @author: SaShuangYiBing Comment: ''' import sys f ...

  4. [部署]VM11下CentOS7mini安装及配置

    最近使用了CentOS发现比Ubuntu更简洁,有些爱上CentOS了 1. 准备一版CentOS安装镜像文件 官网下载地址:http://www.centos.org/download/ 官方有三个 ...

  5. 洛谷P1803

    #include <iostream>#include <algorithm>#include <cstdio>using namespace std; struc ...

  6. Eclipse Mars 2安装Drools6.4插件(Drools and jBPM tools)时无法安装JBoss Runtime Drools Detector

    在eclipse上本地安装Drools6.4Final的时候出现两个组件无法正常安装的情况,具体组件如下: 具体的提示信息为: Cannot complete the install because ...

  7. Spring之强制修改某个方法的行为(Arbitrary method replacement)

      A less commonly useful form of method injection than Lookup Method Injection is the ability to rep ...

  8. python-celery定时提交任务

    pip install celery 使用消息中间件:RabbitMQ/Redis app=Celery('任务名',backend='xxx',broker='xxx') 基本使用 import c ...

  9. [转]OPENCV3.3+CUDA9.0 环境搭建若干错误总结

    编译OpenCV设计启用OpenGL三维可视化支持和启用GPU CUDA并行加速处理的基本知识: 1.从2.4.2版本开始,OpenCV在可视化窗口中支持OpenGL,这就意味着在OpenCV中可以轻 ...

  10. C语言中几种类型所占字节数

    其实C标准并没有具体给出规定哪个基本类型应该是多少个字节数,而且这个也与OS.编译器有关,比如同样是在32位操作系统,VC++的编译器下int类型为4个字节,而在tuborC下则是2个字节. 下面给出 ...