LeetCode OJ-- 3Sum **
https://oj.leetcode.com/problems/3sum/
在一个数列中,求出所有3个数之和是0的3个数。
刚开始给自己挖了坑,由于没闹清,unique()函数的含义,去掉重复不彻底,所以一直 output limit Exceeded.
于是,想新的判断是否重复的方法,这样跳过了一部分之后,再只判断是否,要加入的数据和上一个加入的数据是否相同,就可以了。
- if(i>0 && num[i] == num[i-1])
- continue;
- class Solution {
- public:
- vector<vector<int> > threeSum(vector<int> &num) {
- vector<vector<int> > ans;
- if(num.size()<)
- return ans;
- sort(num.begin(),num.end());
- int k = ;
- for(int i = ;i<num.size()-;i++)
- {
- if(i> && num[i] == num[i-])
- continue;
- int j = i+;
- if(num[i]+num[j]>)
- continue;
- k = num.size()-;
- while(j<k)
- {
- if(num[i]+num[j]+num[k] == )
- {
- if(ans.size()== || ans.size()> && !(num[i]==ans[ans.size()-][]&& num[j] ==ans[ans.size()-][] ))
- {
- vector<int> ansPiece;
- ansPiece.push_back(num[i]);
- ansPiece.push_back(num[j]);
- ansPiece.push_back(num[k]);
- //remove duplicated
- /*bool flag = true;
- for(vector<vector<int> >::iterator it = ans.begin(); it != ans.end(); it++)
- {
- if((*it)[0] == ansPiece[0] && (*it)[1] == ansPiece[1] && (*it)[2] == ansPiece[2])
- {
- flag = false;
- break;
- }
- }
- if(flag)
- ans.push_back(ansPiece);*/
- ans.push_back(ansPiece);
- }
- }
- if(num[i]+num[j]+num[k] < )
- j++;
- else
- k--;
- }
- }
- return ans;
- }
- };
LeetCode OJ-- 3Sum **的更多相关文章
- LeetCode OJ 3Sum 3个整数之和
题意:给一个vector<int>容器,要求每当找到3个元素之和为0时就将这3个数按大小顺序记下来,用一个二维vector返回.也就是vector< vector<int> ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
随机推荐
- relu函数是否存在梯度消失问题以及relu函数的死亡节点问题
relu函数是否存在梯度消失问题以及relu函数的死亡节点问题 存在,在小于的时候,激活函数梯度为零,梯度消失,神经元不更新,变成了死亡节点. 出现这个原因可能是因为学习率太大,导致w更新巨大,使得输 ...
- Beats、Filebea入门
1. Filebeat配置简介 2. Filebeat收集nginx日志 3. packetbeat简介与演示
- hdu3374 String Problem 最小最大表示法 最小循环节出现次数
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...
- Jquery 实现层的拖动,支持回调函数
最近在写一个CMS内容管理系统,前台基本是用ajax异步请求服务器,通过ashx处理,返回json格式处理.由于需要更加人性化的界面,所以采用到了拖动层的操作. 以下是拖动层的主要核心方法,本来想写成 ...
- Python+Selenium中级篇之-封装一个自己的类-浏览器引擎类
前一篇文章我们知道了,如何去封装几个简单的Selenium方法到我们自定义的类,这次我们编写一个类,叫浏览器引擎类,通过更改一个字符串的值,利用if语句去判断和控制启动那个浏览器.这里我们暂时,支持三 ...
- 从输入url开始,完善前端体系架构
原文链接: https://segmentfault.com/a/1190000013662126 从输入URL到页面加载的过程?如何由一道题完善自己的前端知识体系! javascript 前端 23 ...
- Java 第七次
- 【转】Map/Reduce简介
转自:http://blog.csdn.net/opennaive/article/details/7514146 1. MapReduce是干啥的 因为没找到谷歌的示意图,所以我想借用一张Hadoo ...
- python 使用入的坑
如测试代码,并没有将li.li_ 的交集查询出来 li=[1,2,3,4,5] li_=[2,5,6,7,9] for i in li_: if i in li: li_.remove(i) prin ...
- var和function定义方法的区别
在JS中有两种定义函数的方式,1是var aaa=function(){...}2是function aaa(){...}var 方式定义的函数,不能先调用函数,后声明,只能先声明函数,然后调用.fu ...