一天一道LeetCode系列

(一)题目

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.

For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)

(二)解题

这道题的关键在于:结果集合中不允许有重复。

想到的方法有两个:1、在查找过程中去重 2、在结果中去重

经过试验,结果中去重会直接超时。

1、过程中去重版本

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> vecresult;
        std::sort(nums.begin(),nums.end());
        int len = nums.size();
        if(len <3) return vecresult;
        for(int i = 0 ; i < nums.size() ;)
        {
            if(nums[i]>0) return vecresult;
            for(int j = i+1;j<nums.size()-1;)
            {
                int temp = 0-nums[i]-nums[j];
                vector<int>::iterator iter = find(nums.begin()+j+1,nums.end(),temp);
                if(iter != nums.end())
                {
                    vector<int> tempres;
                    tempres.push_back(nums[i]);
                    tempres.push_back(nums[j]);
                    tempres.push_back(*iter);
                    vecresult.push_back(tempres);
                }
                j++;
                while(j<nums.size()&&nums[j]==nums[j-1]) ++j;//去重
            }
            i++;
            while(i<nums.size()&&nums[i]==nums[i-1]) ++i;//去重
        }
        return vecresult;
    }
};

2、结果中去重版本(超时)

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> vecresult;
        std::sort(nums.begin(),nums.end());
        int len = nums.size();
        if(len <3) return vecresult;
        for(int i = 0 ; i < nums.size() ; i++)
        {
            if(nums[i]>0) return vecresult;
            for(int j = i+1;j<nums.size()-1;j++)
            {
                int temp = 0-nums[i]-nums[j];
                vector<int>::iterator iter = find(nums.begin()+j+1,nums.end(),temp);
                if(iter != nums.end())
                {
                    vector<int> tempres;
                    tempres.push_back(nums[i]);
                    tempres.push_back(nums[j]);
                    tempres.push_back(*iter);
                    if(vecresult.size()==0)
                    {
                        vecresult.push_back(tempres);
                    }
                    else{
                        vector<vector<int>>::iterator it1 = find(vecresult.begin(),vecresult.end(),tempres);
                        if(it1 == vecresult.end())//结果中去重
                        {
                            vecresult.push_back(tempres);
                        }
                    }
                }
            }
        }
        return vecresult;
    }
};

【一天一道LeetCode】#15 3Sum的更多相关文章

  1. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  2. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  3. 每日一道 LeetCode (15):二进制求和

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  4. [LeetCode] 15. 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 ...

  5. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

  6. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

  7. LeetCode 15. 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. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

  9. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  10. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

随机推荐

  1. 新浪微博Oauth2.0授权认证及SDK、API的使用(Android)

    ---------------------------------------------------------------------------------------------- [版权申明 ...

  2. Swift3中方法可变参数语法的一些改变

    我们知道在Swift2中,默认情况下方法的参数是let值,也就是不可改变的. 不过我们可以在参数前添加var关键字改变其不变性: func foo(var i:Int){ i += 1 print(i ...

  3. Hibernate之实体关系映射

    延迟加载与即时加载 例如Person类和Email类是一对多关系,如果设为即时加载,当加载Person时,会自动加载Email,如果设置为延迟加载,当第一次调用person.getEmails()时才 ...

  4. VMware 下的CentOS6.7 虚拟机与Windows7通信

    在有网络的情况下,VMware 虚拟机使用桥接模式(Bridged) 和NAT方式,会自动通信,但是在没有网络的情况下怎么办呢?对,是的,使用host-only模式,如何设置呢? 注:将Windows ...

  5. CSDN 支持Markdown写文章了!

    开源中国等其他技术博客很早就支持markdown格式写文章了,今天发现csdn竟然也可以了,不仅支持而且可以在线预览,本地导入导出,远程导入. 这些对于程序员写东西都非常好用,不用总是花时间来排版了. ...

  6. Java的访问权限详解(3+1)public private protected default

    Java使用三个关键字在类的内部设定访问权限:public.private.protected.这些访问指定词(access specifier)决定了紧跟其后被定义的成员(方法或属性)可以被谁使用. ...

  7. Android简易实战教程--第十话《模仿腾讯手机助手小火箭发射详解》

    之前对系统自带的土司的源码做了简要分析,见博客:点击打开链接 这一篇给一个小案例,自定义土司,模拟腾讯卫士的小火箭发射.如果想要迅速看懂代码,建议先去看一下上篇介绍点击打开链接 首先,定义一个服务,在 ...

  8. 1.QT中播放视频,录音程序的编写

     1  通过process的方式播放视频 T22VideoPlayer.pro HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += gu ...

  9. Transform介绍(Unity3D开发之二)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=491 可能unity中接触较早的 ...

  10. android修改HOLO对话框风格

    andriod中修改对话框的风格,可以通过设置theme来实现,部分元素需要通过Java代码来修改,下面以修改对话框的标题为例说明各步骤. 1.编写一个文本样式. DIALOG的标题是一个textvi ...