3Sum - LeetCode
题目链接
注意点
- 和two sum那道题不一样的是这题返回的是具体的数字,不是下标
解法
解法一:将每个数字都作为target,剩下的数字按照two sum那道题来做,得到的结果先排序然后放进set,保证没有重复的结果。因为用了太多STL容器所以...时间复杂度为O(我也不知道怎么算)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int i,n = nums.size(),target;
set<vector<int>> ansSet;
map<int,int> myMap;
vector<int> anw;
for(i = 0;i < n;i++)
{
target = -nums[i];
int j;
for(j = 0;j < n;j++)
{
if(j != i)
{
if(myMap.count(target - nums[j]))
{
anw.push_back(nums[j]);
anw.push_back(target - nums[j]);
anw.push_back(-target);
sort(anw.begin(),anw.end());
ansSet.insert(anw);
anw.clear();
}
myMap[nums[j]] = j;
}
}
myMap.clear();
anw.clear();
}
vector<vector<int>> ans;
n = ansSet.size();
set<vector<int>>::iterator it;
for(it=ansSet.begin();it!=ansSet.end();it++)
{
ans.push_back(*it);
}
return ans;
}
};

解法二:因为这道题返回的是具体的数字,所以可以直接用sort()先对输入进行排序。然后和解法一一样每个数字都作为target,但是不一样的是,因为是有序的数组所以可以维护双指针加快速度,并且跳过重复的数字加快速度,同时因为跳过了重复的数字,就不需要对结果去重了。时间复杂度O(n^2)
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(),nums.end());
int n = nums.size(),target,i = n-1,j,k;
vector<vector<int>> ans;
while(i >= 2)
{
target = -nums[i];
j = 0;
k = i-1;
while(j < k)
{
int temp = nums[j]+nums[k];
if(temp < target)
{
j++;
}
else if(temp > target)
{
k--;
}
else
{
vector<int> v = {nums[i], nums[j], nums[k]};
ans.push_back(v);
j++;
k--;
while(j < k && nums[j-1] == nums[j])
{
j++;
}
while(j < k && nums[k+1] == nums[k])
{
k--;
}
}
}
i--;
while(nums[i+1]==nums[i])
{
i--;
}
}
return ans;
}
};

小结
- 慎用stl容器!不然你不知道你的程序时间复杂度和空间复杂度会变成什么鬼样子
3Sum - LeetCode的更多相关文章
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- 3Sum——leetcode
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 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 ...
- LeetCode 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
随机推荐
- opengl矩阵向量
如何创建一个物体.着色.加入纹理,给它们一些细节的表现,但因为它们都还是静态的物体,仍是不够有趣.我们可以尝试着在每一帧改变物体的顶点并且重配置缓冲区从而使它们移动,但这太繁琐了,而且会消耗很多的处理 ...
- jmeter控制器(二)
循环控制器: 顾名思义就是做循环控制的,与线程组的循环一样的,不过这里的循环控制器是用在一个单独的模块的,而在线程组里面的循环是作用于全局的.循环控制器里面设置的循环次数是局部有效,只控制自己范围内的 ...
- java多线程之述解
说起线程 就不得不提进程 他们之间的关系很紧密 进程:内存中运行的应用程序 每个进程都有自己的一块内存空间 而线程是进程中的一个执行单元 一个进程中可以有多个线程 多线程的好处就是可以并发操作程序 将 ...
- 【Python学习笔记】正则表达式
Ref:https://deerchao.net/tutorials/regex/regex.htm#greedyandlazy 1. 常用元字符 2.字符转义 查找元字符本身时,需要使用\来取消这些 ...
- 配置tensorflow环境(anaconda+jupyter notebook)
很早之前,tensorflow环境之前我也曾装过,但是用的不是很舒服,很多问题都不明所以然.今天想要系统地学习一下tensorflow,于是又重新搭建了一遍,这次还是踩了不少坑.特此写下此文,供有兴趣 ...
- import 导入包的特别用法总结
指定别名 可以为包指定一个别名,以便记忆或提高输入效率 如 import str "strings" 在使用的时候可以直接使用别名,如原先要写成strings.Contains,现 ...
- Vue+webpack报错: listen EADDRINUSE: address already in use :::8080
如果本地运行多个vue+webpack项目会报错:listen EADDRINUSE: address already in use :::8080: 提示含义:地址端口已经被占用 注:8080指的是 ...
- Linux 定时清理日志脚本
在远程运行节点创建一个cleanlog.sh 脚本文件 vin clenalog.sh 插入以下内容 #!/bin/env bash start=$(date +%y-%m-%d-%H%M%m) Fi ...
- 课堂讨论—Alpha版总结会议
我们在课堂上针对第一阶段冲刺过程中存在的问题,展开了激烈的讨论,并投票选出需要改进的最主要三个问题. 有图有真相:
- 校友聊---Sprint计划会议总结
1.产品需求及索引卡: 校友聊的软件我们计划分三步进行设计实现功能:文字聊天.语音聊天.视频聊天.首先第一步我们要实现文字聊天这个功能. 经过调研讨论之后,确定了产品的几个需求:在局域网内实现通信要依 ...