题目链接

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的更多相关文章

  1. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

  2. 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 ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  5. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  6. [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 ...

  7. [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 ...

  8. LeetCode 3Sum Smaller

    原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...

  9. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

随机推荐

  1. node.js主从分布式爬虫

    前言 前文介绍过用Python写爬虫,但是当任务多的时候就比较慢, 这是由于Python自带的http库urllib2发起的http请求是阻塞式的,这意味着如果采用单线程模型,那么整个进程的大部分时间 ...

  2. vue 组件-组件定义的4种方式

    一.组件命名的方式 ①kebab-case,单词之间采用  - (短横线)连接,例如:my-component ,在DOM中使用时,<my-component ></my-compo ...

  3. Zabbix对接AD域

    需要的信息:一个域账号密码,使用的端口:域名   1.先查看php是否安装了ldap模块 php –m (查看已安装的php模块)   若没有安装请参照“在已编译安装的PHP环境下安装LDAP模块”. ...

  4. 记一次nginx -t非常慢的排障经历

    在一次修改nginx配置时候,执行 case: #/usr/local/nginx/sbin/nginx -t 出现执行命令出现很久没返回结果,也没返回成功或是失败,就是一直卡住的状态,严重影响ngi ...

  5. Scrum Meeting 12 -2014.11.18

    今天的任务都比较顺利,测试暂时还没发现特别的问题. Member Today’s task Next task 林豪森 与其他小组商讨整合问题 与其他小组商讨整合问题 宋天舒 测试项目功能实现 测试项 ...

  6. 10.29 scrum meeting newbe软件团队工作分配

    这次会议,我们主要讨论了目前阶段的主要任务与任务分配问题. 首先,通读代码,理解程序的运行方式是必不可少的环节.所以我们要求团队的所有成员通读代码.并且对于开发人员和测试人员,要求写出我们分配的各自模 ...

  7. dazhewang数据库初设计

    mysql> use dazhe; Database changed mysql> create table shops(id int primary key auto_increment ...

  8. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  9. Beta版本测试第二天

    一. 每日会议 1. 照片 2. 昨日完成工作 登入界面的优化与注册界面的优化,之前的登入界面与注册界面没有设计好,使得登入界面与注册界面并不好看,这次对界面进行了优化.另外尝试了找回密码的功能. 3 ...

  10. 利用css制作带边框的小三角

    标签(空格分隔):css 在项目中会使用到的小实例,目前知道的有两种方法来实现 设置元素的宽和高,利用rotate实现,比较简单的一种 div{ width: 10px; height: 10px; ...