Given an array nums of n integers, are there elements a, b, c 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]
]
想法:对数组进行排序。固定一个位置,然后使用两个指针,一个执行固定点后一个位置,另外一个指向尾端元素。然后判断三个位置的元素和是否为0.如果为0
添加到vector中,如果和大于0,指向尾端元素的指针执行自减操作。如何和小于0,指向固定点位置后一个元素的指针执行自增操作.
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int len = nums.size();
        vector<vector<int>> result;
         == len)
            return result;
        sort(nums.begin(),nums.end());
         ; i < len ; i++){
            ;
            ;
             && nums.at(i) == nums.at(i-))
                continue;//跳过重复情况
            while(front < end ){
                ){
                  result.push_back({nums.at(i),nums.at(front),nums.at(end)});
                    front++;
                    end--;
                    ))
                        front++;//避免front对应的值重复
                    ))
                        end--;//避免end对应的值重复

                }){
                    front++;
                }else{
                    end--;
                }
            }
        }
        return result;

    }
};

leetcode15—3Sum的更多相关文章

  1. LeetCode15 3Sum

    题意: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  2. LeetCode15——3Sum

    数组中找三个数和为0的结果集 1 // 解法一:先排序 然后固定一个值 然后用求两个数的和的方式 public static List<List<Integer>> three ...

  3. Leetcode15.3Sum三数之和

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

  4. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  5. ARTS第六周

    第六周.后期补完,太忙了. 1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有 ...

  6. [Swift]LeetCode15. 三数之和 | 3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

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

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

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

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

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

随机推荐

  1. 服务注册中心Eureka vs Zookeeper vs Consul

    前言 在现在云计算和大数据快速发展的今天,业务快速发展和变化.我们以前的单一应用难以应对这种快速的变化, 因此我们需要将以前单一的大应用不断进行差分,分成若干微小的应用或者服务,这就是微服务的思想.但 ...

  2. ORACLE查看数据库已安装补丁

    cd $ORACLE_HOME ./opatch lsinventory :}

  3. Python paramiko ssh 在同一个session里run多个命令

    import threading, paramiko strdata='' fulldata='' class ssh: shell = None client = None transport = ...

  4. JS 回调函数、立即执行、for块作用域、try/catch、let、垃圾收集 p3

    限于时间关系,加上有些倦意,简单的记录下一些要点: 1.回调函数:就你把函数当成参数传给另一个函数,这个函数在某个时间段会执行这个函数.

  5. vue.js自定义指令详解

    写在文本前:相信在做vue的项目,你肯定接触了指令,我们常用vue内置的一些指令,比如v-model,v-text,v-if,v-show等等,但是这些内置指令不在本文的讲解范畴,本文想说的是其自定义 ...

  6. linux下lamp环境修改网站根目录

    Apache默认的网站目录是在/var/www/html,我们现在要把网站目录更改成 /var/www 目录下,操作如下: 1.修改httpd,conf文件 vi /etc/httpd/conf/ht ...

  7. [基础架构]PeopleSoft Web Server 重要文件说明

    我们都知道PeopleSoft是由几个不同的服务组成的,他们在PeopleSoft体系结构中扮演着自己的角色.这些服务具有不同的文件结构并包含重要的可执行文件和配置文件. 以下是Peoplesoft体 ...

  8. deep learning自学知识积累笔记

    推荐系统的演变过程 协同过滤(英雄所见略同)思想为类似喜好的人的选择必然也类似.比如小学男生普遍喜欢打手游,中年大叔普遍喜欢射雕英雄传 随后有了SVD奇异值分解,但是SVD要求不能太稀疏,因此有了隐语 ...

  9. python 流程控制(while)

    1,while基本语法 2,while else语句 1,while基本语法 n = 1 while n<10: print n n += 1 2,while else语句 n =10 whil ...

  10. 关于<asp:checkBoxList>控件的对齐方法

    定义和用法 TextAlign 属性用于获取或设置 CheckBoxList 项目的文本的文本对齐方式. 语法 <asp:CheckBoxList TextAlign="align&q ...