1.题目描述

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

 

Note:

 

Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ? b ? c)

The solution set must not contain duplicate triplets.

    For example, given array S = {-1 0 1 2 -1 -4},

 

    A solution set is:

    (-1, 0, 1)

    (-1, -1, 2)

2.解法分析

之前做过3sum closest的题目,很显然,那里的思路应用到这里是绝对可行的, 但是这个题目我觉得可以用hashmap来做,结果就写了个基于hash的程序,可是结果总是差点,检查了好半天没检查出来,先记录一下

class Solution {

public:

 

    vector<vector<int> > threeSum(vector<int> &num) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        vector<vector<int> > result;

        int numSize=num.size();

        if(numSize<2)return result;

        

        sort(num.begin(),num.end());

        unordered_multiset<int> myHash;

        

        for(int i =0;i<num.size();++i)

        {

            myHash.insert(num[i]);

        }

        

        int thirdNum=0;

        vector<int>cur;

        cur.assign(3,1);

        

        for(int i=0;i<num.size()-2;++i)

        {

            if(i>0&&num[i-1]==num[i])break;

            if(num[i]>0)break;

            

            if(num[i]!=num[i+1]&&myHash.count(num[i])>0)myHash.erase(num[i]);

            for(int j=i+1;j<num.size()-1;++j)

            {

                thirdNum=0-num[i]-num[j];

                if(thirdNum<num[j])break; 

                if(num[j+1]!=num[j])myHash.erase(num[j]);

                if(myHash.count(thirdNum)>0)

                {

                    if(cur[0]!=num[i]||cur[1]!=num[j]||cur[2]!=thirdNum)

                    {

                        cur[0]=num[i];cur[1]=num[j];cur[2]=thirdNum;

                        result.push_back(cur);

                    }      

                }

              

            }

        }

        

        

        return result;

    }

    

 

};

leetcode—3sum的更多相关文章

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

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

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

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

  4. LeetCode 3Sum Smaller

    原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...

  5. leetcode — 3sum

    import java.util.*; /** * Source : https://oj.leetcode.com/problems/3sum/ * * Created by lverpeng on ...

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

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

  9. Leetcode 3Sum Closet

    二手和3Sum像几乎相同的想法.二进制搜索.关键修剪.但是,在修剪做出很多错误. 然后还有一个更加速了原来的想法O(n^2). #include<iostream> #include &l ...

随机推荐

  1. arcgis api for javascript 出现 undefinedModule错误

    一般都是script代码里面语法错误,如. .;:之类的

  2. JS中用execCommand("SaveAs")保存页面兼容性问题解决方案

    开发环境:ASP.NET MVC,其他环境仅供参考. 问题描述:在开发中遇到这样的需求,保存页面,通常使用JavaScript的saveAs进行保存,各浏览器对saveAs支持,见下表. 代码一:初始 ...

  3. JVM最多可创建多少线程

    JVM可支持的最大线程数 JVM最大线程数 (2012-07-04 23:20:15) 转载▼ 标签: jvm 最大线程数 it 分类: java分布式总结 摘自:http://sesame.itey ...

  4. HttpServletRequest接口实例化的使用

    HttpServletRequ接口的使用和jsp内置对象的request对象非常类似,request对象其实 就是HttpServletRequest接口的一个实例,不过气实例化的过程是自动的,无须自 ...

  5. linux 免交互状态下修改用户密码

    当利用某些工具对linux用户进行远程密码更改时,输入[ passwd 用户名 ] 后需要输入两次密码, 但是如果你利用的某些工具无法与linux进行交互的情况下,就没办法变更用户密码了,这个时候可以 ...

  6. win7进入不了系统故障修复

    问题: 由于电脑关机比较慢,等得不耐烦了,就强制关机了,以前都没事,直到昨晚打开电脑,提示windows错误恢复,试了好久,提示windows无法修复此计算机,看来是没办法了.后来进入系统还原后,总算 ...

  7. jquery.post用法

    使用ajax在前端验证是否执行某个操作 jquery.post参数为 url,[data],[callback],[type] url:发送请求地址. data:待发送 Key/value 参数. c ...

  8. C#版二维码生成器

    前言 本文所使用的二维码生成代码是谷歌开源的条形码图像处理库完成的,c#版的代码可去 这里   --  下载压缩包. 截止目前为止最新版本为2.2,提供以下编码格式的支持: UPC-A and UPC ...

  9. Dozer应用——类之间值的映射

    1. Mappings via Annotation public class SourceBean { private Long id; private String name; @Mapping( ...

  10. 字符设备 register_chrdev_region()、alloc_chrdev_region() 和 register_chrdev()

    1. 字符设备结构体 内核中所有已分配的字符设备编号都记录在一个名为 chrdevs 散列表里.该散列表中的每一个元素是一个 char_device_struct 结构,它的定义如下: static ...