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. 如何解决VMware 虚拟机不能铺满屏幕

    出现这种情况是因为分辨率设置问题,调整分辨率总能达到你想要的效果: 首先设置vmware为全屏模式 查看物理机的分辨率,然后再将虚拟机的分辨率设置了跟物理机的一致.完美解决 windows10查看分辨 ...

  2. HDU6195

    cable cable cable Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. js飘窗

    广告页上总会出现飘窗效果: adver_pos_id = getOtherParameter("id"); adver_Sid = getOtherParameter(" ...

  4. js-ES6学习笔记-编程风格(2)

    1.那些需要使用函数表达式的场合,尽量用箭头函数代替.因为这样更简洁,而且绑定了this. 2.所有配置项都应该集中在一个对象,放在最后一个参数,布尔值不可以直接作为参数. 3.不要在函数体内使用ar ...

  5. lastIndex()与IndexOf()的区别

    lastIndex()与IndexOf()的区别 str.indexOf() indexOf()方法返回某个指定的字符串值在字符串中首次出现的位置(从左向右).没有匹配的则返回-1,否则返回首次出现位 ...

  6. JS函数机制小结

    1.javascript中函数是第一型对象,即与其它对象一样,具有: 1.可以通过字面量创建 2.可以赋值给变量或者属性 3.可以作为参数进行传递 4.可以作为函数结果返回 5.拥有属性和方法 2.函 ...

  7. 【VS2015】Win7 X64上面安装VS2015

    环境: 1.Win7 x64 SP1旗舰版 2.VS2015专业版Update3 3.IE11 4.WDK10 5.SDK10   安装步骤: 1.安装IE11,需要如下补丁:     a.Windo ...

  8. JAVA:函数的定义、使用

      本文内容: 什么是函数 函数的定义格式 函数的重载(overload): 函数的调用使用注意 关于形式参数的使用 首发时间:2017-06-23 修改时间:2018-03-21:修改了布局,修改了 ...

  9. AForge.NET简介

    AForge.NET是一个专门为开发者和研究者基于C#框架设计的,这个框架提供了不同的类库和关于类库的资源,还有很多应用程序例子,包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,机器 ...

  10. SQLSERVER将数据移到另一个文件组之后清空文件组并删除文件组

    SQLSERVER将数据移到另一个文件组之后清空文件组并删除文件组 之前写过一篇文章:SQLSERVER将一个文件组的数据移动到另一个文件组 每个物理文件(数据文件)对应一个文件组的情况(一对一) 如 ...