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. 一个zip压缩类,欢迎吐槽

    package com.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import j ...

  2. 对Memcached使用的总结和使用场景

    1.memcached是什么 Memcached 常被用来加速应用程序的处理,在这里,我们将着重于介绍将它部署于应用程序和环境中的最佳实践.这包括应该存储或不应存储哪些.如何处理数据的灵活分布以 及如 ...

  3. 卷积神经网络Convolutional Neural Networks

    Convolutional Neural Networks NOTE: This tutorial is intended for advanced users of TensorFlow and a ...

  4. POJ 2114 Boatherds【Tree,点分治】

    求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...

  5. hdu 4941 Magical Forest ( 双重map )

    题目链接 题意: 有一个n*m的田地,里边有k棵树,每棵树的位置为(xi,yi),含有能量值ci.之后又q个询问,分三种; 1)1 a b,将a行和b行交换 2)2 a b,将a列和b列交换 3)3 ...

  6. Sass结合Modernizr的使用方法

    Modernizr在初始化的时候会首先找寻class=“no-js”的元素: <!DOCTYPE html> <html class="no-js"> &l ...

  7. Asp.Net生成RSS方法

    一.RSS简介 什么是RSS? RSS是一种网页内容联合格式(web content sydication format). 它的名字是Really Simple Syndication的缩写. RS ...

  8. I.MX6 默认打开 USB adb

    /***************************************************************************** * I.MX6 默认打开 USB adb ...

  9. 流媒體】jrtplib—VS2010下RTP开源协议库JRTPLIB3.9.1编译

    一.JRTPLIB简介 老外用C++编写的开源RTP协议库,用来进行实时数据传输,可以运行在 Windows.Linux. FreeBSD.Solaris.Unix和VxWorks 等多种操作系统上, ...

  10. java 异常java.lang.UnsupportedOperationException

    在项目中采用一个枚举的集合,本人采用Collections中的空集合Collections.emptyList()在添加时发生异常: 常见集合如下: private List<VacationC ...