思路
  • 直接暴力,\(O(n^4 log(n))\),不出意外的超时了。。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res; int i,j,k;
int len = nums.size();
for(i = 0; i < len; i++){
for(j = i+1; j < len; j++){
for(k = j+1; k < len; k++){
if(nums[i]+nums[j]+nums[k] == 0){
vector<int> inRes;
inRes.push_back(nums[i]);
inRes.push_back(nums[j]);
inRes.push_back(nums[k]);
bool ok = true;
for(int m = 0; m < res.size(); m++){
if(issame(inRes,res[m])){
ok = false;
continue;
}
}
if(ok) res.push_back(inRes);
}
}
}
}
return res;
}
bool issame(vector<int> a,vector<int> b){
if(a.size() != b.size()) return false;
sort(begin(a),end(a));
sort(begin(b),end(b));
bool ok = true;
for(int i = 0; i < a.size(); i++){
if(a[i] != b[i]){
ok = false;
}
}
return ok;
}
};
  • 利用map暴力,时间复杂度 \(O(n^3 log(n))\),依然超时。。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
map<int,int> tmp;
int i,j,k;
int len = nums.size();
for(i = 0; i < len; i++){
tmp[nums[i]] = i;
}
for(i = 0; i < len; i++){
for(j = i+1; j < len; j++){
vector<int> inRes = {};
if(tmp.find(-(nums[i]+nums[j])) != tmp.end() && tmp[-(nums[i]+nums[j])] != i && tmp[-(nums[i]+nums[j])] != j){
inRes.push_back(nums[i]);
inRes.push_back(nums[j]);
inRes.push_back(-(nums[i]+nums[j]));
bool ok = true;
for(int m = 0; m < res.size(); m++){
if(issame(inRes,res[m])) ok = false;
}
if(ok){
res.push_back(inRes);
}
} }
}
return res;
}
bool issame(vector<int> a,vector<int> b){
if(a.size() != b.size()) return false;
sort(begin(a),end(a));
sort(begin(b),end(b));
bool ok = true;
for(int i = 0; i < a.size(); i++){
if(a[i] != b[i]){
ok = false;
}
}
return ok;
}
};
  • 利用3个指针,首先固定一个,然后类似于Two Sum问题。(这里是用map存储返回结果的,需要避免重复的问题,而直接用set存储就方便许多了),复杂度为 \(O(n^2)\),如果用set的话,复杂度应该是 \(O(n^3)\)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int i,lo,hi;
int len = nums.size();
sort(nums.begin(), nums.end());
vector<vector<int>> res; for(int i = 0; i < len-2; i++){
int lo = i+1,hi = len-1;
int sum = -nums[i];
if(i == 0 || (nums[i] != nums[i-1] && i > 0)){
while(lo < hi){
if((nums[lo] + nums[hi]) == sum){
vector<int> tmp;
tmp.push_back(nums[i]);
tmp.push_back(nums[lo]);
tmp.push_back(nums[hi]);
cout << tmp[0] << " " << tmp[1] <<" " << tmp[2] << endl;
res.push_back(tmp);
while(lo < hi && nums[lo] == nums[lo+1]){
lo++;
}
while(lo < hi && nums[hi] == nums[hi-1]){
hi--;
}
lo++;hi--;
}
else if ((nums[lo]+nums[hi]) < sum)
{
lo++;
}else hi--;
}
}
}
return res;
}
};

15.Three Sum的更多相关文章

  1. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  2. LeetCode#15 | Three Sum 三数之和

    一.题目 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意:答案中不可以包含 ...

  3. 【leetcode】15. 3 Sum 双指针 压缩搜索空间

    Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i ...

  4. oracle 按时间段统计15分钟内的数据

    string sql = "select to_char(StartTime, 'yyyy')||'-'|| to_char(StartTime, 'mm')||'-'|| to_char( ...

  5. oracle中sum求和问题

    如列表所示:都是选填字段name   age salary weight张三     18      20李四     17王五     21燕小六  15      22 sum(age+salar ...

  6. sum()函数——MATLAB

    a=sum(A)  %列求和 b=sum(A,2) %行求和 c=sum(A(:)) %矩阵求和 假定A为一个矩阵: sum(A)以矩阵A的每一列为对象,对一列内的数字求和. sum(A,2)以矩阵A ...

  7. 08-numpy-笔记-sum

    求和: axis = 0 按列求和 axis = 1 按行求和 >>> import numpy as np >>> a = np.mat([[1,2,3],[4, ...

  8. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  9. java多线程系类:JUC线程池:06之Callable和Future(转)

    概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...

随机推荐

  1. 篇3 安卓app自动化测试-搞定界面元素

    篇3                 安卓app自动化测试-搞定界面元素 --lamecho辣么丑 1.1概要 大家好! 我是lamecho(辣么丑),今天是<安卓app自动化测试>的第三 ...

  2. AOJ/初等排序习题集

    ALDS1_1_D-MaximumProfit. Codes: //#define LOCAL #include <cstdio> #include <algorithm> u ...

  3. 设计模式的征途—3.抽象工厂(Abstract Factory)模式

    上一篇的工厂方法模式引入了工厂等级结构,解决了在原来简单工厂模式中工厂类职责太重的原则,但是由于工厂方法模式的每个工厂只生产一类产品,可能会导致系统中存在大量的工厂类,从而增加系统开销.那么,我们应该 ...

  4. 读 zepto 源码之工具函数

    Zepto 提供了丰富的工具函数,下面来一一解读. 源码版本 本文阅读的源码为 zepto1.2.0 $.extend $.extend 方法可以用来扩展目标对象的属性.目标对象的同名属性会被源对象的 ...

  5. DirectFB 之 通过多Window实现多元素处理

    图像 设计 采用多window的方式实现显示,因为每个window可以独立的属性,比如刷新频率,也是我们最关注的 示例 /*************************************** ...

  6. 关于在"a"标签中添加点击事件的一些问题

    昨天做修改页面跳转时遇到一个问题,如果a标签的"href"属性为空的话,比如这样<a href="" onclick="roleupdate() ...

  7. OC 动态类型和静态类型

    多态 允许不同的类定义相同的方法 动态类型 程序直到执行时才能确定所属的类 静态类型 将一个变量定义为特定类的对象时,使用的是静态形态 将一个变量定义为特定类的对象时,使用的是静态类型,在编译的时候就 ...

  8. git pull冲突:commit your changes or stash them before you can merge.

    今天用git pull来更新代码,遇到了下面的问题: error: Your local changes to the following files would be overwritten by ...

  9. 利用反射来实现获取成员的指定特性(Attribute)信息

    在开发过程中,我们经常需要自定义一些特性,来辅助我们完成对对象或者枚举进行管理.我们需要知道如何获取对象使用的特性信息. 以下举个学习用的例子. 我们自定义一个特性类,这个特性设置在一个数据段内是否执 ...

  10. Unity非运行模式下实现动画播放/回退工具

    实现效果 核心功能 支持选定模型(带Animator)在非运行模式下,播放/暂停/停止动作. 支持动作单帧前进,单帧回退(帧时间默认0.05f,可以代码设置). 支持滚动条拖拽,将动作调整到指定时间. ...