题目链接

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. Android Library开发注意事项

    Android Library开发注意事项 App Module添加依赖Android Library时可以设置library的优先级, 在编译时,app按照library从低到高的优先级依次与每个l ...

  2. 琴声不等式--jensen

    (来自百度百科) 1. 凹函数,不加权 2. 凹函数,加权 3. 凸函数,不加权 4. 凸函数,加权 应用 在EM算法Q函数的推导中,用到了第二个不等式(凹函数,加权)

  3. 时间戳使用的bug,你见过么

    博主本人前几天给公司项目里写了个禁言和解除禁言的功能,项目中涉及到对时间的处理,因为学得时候也没很注意,就按自己的思路去写了,运行起来发现了一个天大的bug,就是写的延后一年尽然,刚开始就执行了,而且 ...

  4. Kubernetes探索学习003--关于Kubernetes的Pod

    关于Pod 关于Pod我们要慢慢去体会去接受它去使用它,尤其是运维人员这块需要从逻辑上形成认识,首先理解Pod是Kubernetes项目的原子调度单位.为什么是Pod而不是单个DockerContai ...

  5. 部署mysql版本项目问题记录

    一,com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure报错 将url从jdbc:mysq ...

  6. bing词典vs有道词典对比测试报告——功能篇之细节与用户体验

    之所以将细节与用户体验放在一起讨论,是因为两者是那么的密不可分.所谓“细节决定成败”,在细节上让用户感受方便.舒适.不费心而且温馨,多一些人文理念,多一些情怀,做出来的产品自然比其他呆板的产品更受欢迎 ...

  7. 11.15 Daily Scrum

    今天是假期回来的第一个周末,也是我们团队的又一次进度汇总总结和调试工作开展,鉴于一周以来大家的工作有了很大的成果,所以,本次召开的会议主旨在于解决一些开发方面的细节问题,达成共识,为日后进一步的功能方 ...

  8. 四则运算2及PSP0设计项目计划

    时间比较紧,我简单写写我的设计思路: 题目在四则运算1的基础上控制产生题目的数量,这个可以用变量控制:打印方式也可选用变量控制,程序的关键是括号的生成.我们可以将整个四则运算式看成()+()的模型,然 ...

  9. JS学习:JavaScript的核心

    分享到 分类 JS学习   发布 ourjs  2013-12-02 注意 转载须保留原文链接,译文链接,作者译者等信息.     作者: JeremyWei  原文: JavaScript The ...

  10. Alpha版本冲刺(四)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:何家伟 组员8:政演 组员9:鸿杰 组员10:刘一好 组员:何宇恒 展示组内最新 ...