一天一道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. Dynamics CRM build numbers

    Dynamics CRM build numbers CRM各大版本及补丁列表,整理的很全

  2. 开源框架Volley的使用《二》[NetWorkImageView&&LruCache&ImageLoader]

    转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:小杨的博客    http://blog.csdn.net/qq_32059827/article/details/5278849 ...

  3. FORM界面批量处理-全选框实现

    全选框实现方法多种多样,这里只介绍两种 方法一:触发器式,优点程序简单,缺点颜色单调不突出 1.      在数据块和控制块上分别创建check box 2.      设置check box选中与为 ...

  4. iOS网络基础

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51376048 本文出自:[openXu的博客] 常用类 get请求 post请求 NSURL ...

  5. SQLite 附加数据库(http://www.w3cschool.cc/sqlite/sqlite-attach-database.html)

    SQLite 附加数据库 假设这样一种情况,当在同一时间有多个数据库可用,您想使用其中的任何一个.SQLite 的 ATTACH DTABASE 语句是用来选择一个特定的数据库,使用该命令后,所有的 ...

  6. Android Multimedia框架总结(三)MediaPlayer中创建到setDataSource过程

    转载请把头部出处链接和尾部二维码一起转载,本文出自:http://blog.csdn.net/hejjunlin/article/details/52392430 前言:前一篇的mediaPlayer ...

  7. ssh远程登录操作 和ssh信任

    ssh 可以参考上一篇telnet的文章 1.安装openssh-server     sudo dpkg -i openssh-client_1%3a5.5p1-4ubuntu6_i386.deb ...

  8. JQuery插件使用之Validation 快速完成表单验证的几种方式

    JQuery的Validation插件可以到http://plugins.jquery.com/上去下载.今天来分享一下,关于这个插件的使用. 简易使用 这第一种方式可谓是傻瓜式的使用,我们只需要按照 ...

  9. Android View框架总结(五)View布局流程之Layout

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52216195 View树的Layout流程 View的Layout时序图 View布局 ...

  10. 安卓中的消息循环机制Handler及Looper详解

    我们知道安卓中的UI线程不是线程安全的,我们不能在UI线程中进行耗时操作,通常我们的做法是开启一个子线程在子线程中处理耗时操作,但是安卓规定不允许在子线程中进行UI的更新操作,通常我们会通过Handl ...