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;
}
};

最新文章

  1. Model &amp; ModelMap &amp; ModelAndView 比较ModelFactory简介
  2. PostgreSQL 在centos 7下的安装配置
  3. IIS下打印报表到Excel
  4. 破解 RCA_CRACKME(解除隐藏按钮)
  5. 在MAC OS X下安装usb转串口驱动(PL2303主控芯片)
  6. XE5 Android 开发数据访问server端
  7. cocos2dx 坐标和锚点
  8. SpringMVCURL请求到Action的映射规则
  9. mac 访问mysql客户端
  10. JAVA入门[17]-ControllerAdvice处理exception
  11. October 23, 2013 - Fires and smoke in eastern China
  12. Socket端口复用
  13. alignedReID: surpassing human-level performance in person re-identification (paper reading)
  14. 【Java】 剑指offer(16) 打印1到最大的n位数
  15. [Html] jQuery Grid
  16. linux shell 语法学习
  17. EntityFramework Core 2.x (ef core) 在迁移中自动生成数据库表和列说明
  18. TVB三个台
  19. Oracle输出数字1-10
  20. 一次安装win10 ubuntu16.0经过记录

热门文章

  1. (转) Redis哨兵的详解
  2. Diycode开源项目 BaseApplication分析+LeakCanary第三方+CrashHandler自定义异常处理
  3. JVM——自定义类加载器
  4. Android开发环境安装经验
  5. 数据库脚本开发日志模板 —— 项目需求 A
  6. 使用 Spirit 类在 XNA 中创建游戏中的基本单位精灵(十三)
  7. Python+Selenium练习篇之11-浏览器上前进和后退操作
  8. python深浅拷贝以及数据在内存中储存方法
  9. ThinkPHP5 配置文件
  10. 导入50G文件到mysql,然后再倒入sqlserver