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. Qt网络通信骨架解析,QtClient QtServer QtSerialPort

    http://blog.csdn.net/Dr_Abel/article/details/52469134#t18

  2. AspectJ 出现错误::0 can't find referenced pointcut 的解决之道

    使用AspectJ注解开发AOP应用时,会遇到以下问题: ::0 can't find referenced pointcut 这个问题,与你所在的开发环境有关,如下表 jdk version spr ...

  3. cmd命令行指定系统延迟关机时间

    shutdown -s -t 3600 -c "想要显示的注释" -f 各参数的意思:-s 动作为关机 -t 3600 延迟3600秒关机 -c "想要显示的注释&quo ...

  4. 3、JPA一些常用的注解

    常用注解有下面这些: ①:@Entity.@Table.@Id.@GeneratedValue.@Column.@Basic ②:@Transient 用于忽略某个属性,而不对该属性进行持久化操作 ③ ...

  5. spring cloud config 入门

    简介 Spring cloud config 分为两部分 server client config-server 配置服务端,服务管理配置信息 config-client 客户端,客户端调用serve ...

  6. JBPM4 常用表结构

    JBPM4 常用表结构 第一部分:表结构说明 Jbpm4 共有18张表,如下,其中红色的表为经常使用的表   一:资源库与运行时表结构 1.  JBPM4_DEPLOYMENT 流程定义表 2.  J ...

  7. HS-T912 adb 连接配置

    手机丢了,花300大洋买的新手机阿...不讨论好不好用,只说怎么用. 安装驱动 linux 跳过 插上电脑,在__通知__里面打开__USB 连接__,会弹出__USB 设置__对话框. 选择__海信 ...

  8. Android Studio 我常用快捷键

    0. Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类 1. 重载方法 Ctrl+O 2.Ctrl+shift+Enter:自动匹配相对应的语法结构,比如if,do-while,t ...

  9. uva 11624 Fire!(搜索)

    开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...

  10. 【Unity3D】生成工程报错解决—UnityEditor.HostView:OnGUI() Error building Player: Couldn't build player because of unsupported data on target platform.

    错误 错误1:An asset is marked as dont save, but is included in the build: unityEditor.HostView:OnGUI() 错 ...