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. 88. Merge Sorted Array

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume ...

  2. C++:调整基类成员在派生类中的访问属性的其他方法(同名成员和访问声明)

    4.3 调整基类成员在派生类中的访问属性的其他方法 4.3.1 同名函数 在定义派生类的时候,C++语言允许在派生类中说明的成员与基类中的成员名字相同,也就是 说,派生类可以重新说明与基类成员同名的成 ...

  3. Android handler.obtainMessage()

    在handler.obtainMessage()的参数是这样写的: Message android.os.Handler.obtainMessage(int what, int arg1, int a ...

  4. 使用phantomjs实现highcharts等报表通过邮件发送

    使用phantomjs实现highcharts等报表通过邮件发送(本文仅提供完整解决方案和实现思路,完全照搬不去整理代码无法马上得到效果)   前不久项目组需要将测试相关的质量数据通过每日自动生成报表 ...

  5. 转:linux下Xampp安装与配置

    --转载时请保留下面,以供大家加我MSN,增强交流,共同学习.--姜庭华  msn: jaimejth@live.cn--博客:http://blog.csdn.net/jaimejth 软件下载在以 ...

  6. android bitmap out of memory总结、心得

    setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,这些函数在完成decode后,最终都是通过java层的creat ...

  7. C/C++面试题(一)

    1.手写快速排序 void quick_sort(int s[], int l, int r) { if (l < r) { //Swap(s[l], s[(l + r) / 2]); //将中 ...

  8. jsp获取struts2查询到的数据

    1.在struts2的方法中利用查询语句查询到结果,然后再对结果进行封装,jsp根据封装获取相关信息 struts2的查询和封装方法: private List<FreeMarkerDetail ...

  9. C# 计时器的三种使用方法

    在.net中有三种计时器,一是System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet;二是System.Timers命名空间下的Timer类. Timer控件 ...

  10. 在XML里的XSD和DTD以及standalone的使用3----具体使用详解

    本人亲自写的一个简单的测试例子 1.xsd定义 <?xml version="1.0" encoding="utf-8"?><xs:schem ...