题目链接

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. SICP读书笔记 2.1

    SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...

  2. 单纯形法MATALAB实现

    参考单纯形法的步骤,MATALAB中的实现如下(求极小值): 注:对于极大值的求解,只需要对目标函数添加负号,求解出来的\(X\),再带入原目标函数即可. function [ X, z ] = si ...

  3. docker 从本地拷贝文件

    1.找到docker的ID全称 docker inspect -f '{{.Id}}' docker_name 2.执行拷贝命令 docker cp 本地文件路径 ID全称:docker路径 3.如果 ...

  4. ats Linux Bridge内联

    Linux可以配置为在桥接模式下运行. 为网桥分配了两个或更多物理接口. 在接口之间共享单个IP地址. 默认情况下,任何到达一个接口的数据包都会立即路由到另一个网桥接口. 需要的Linux包: bri ...

  5. Erlang数据类型的表示和实现(1)——数据类型回顾

    本文介绍 Erlang 语言中使用的各种数据类型以及这些数据类型在 Erlang 虚拟机内部的表示和实现.了解数据类型的实现可以帮助大家在实际开发过程中正确选择数据类型,并且可以更好更高效地操作这些数 ...

  6. iOS 开发中,关于xxx.xcodeproj 文件冲突的解决方案 (以后谁不会了,直接将连接给他)

    iOS 开发中,关于xxx.xcodeproj 文件冲突的解决方案 (一有冲突要手把手教一遍,太麻烦了,现在总结下,以后谁不会了,连接直接发他). 关于xxx.xcodeproj 文件冲突的话,是比较 ...

  7. 实验三 敏捷开发和XP实验

    课程:Java程序设计实验   班级:1352             姓名: 于佳心           学号:20135206 成绩:               指导教师:娄嘉鹏         ...

  8. 实验1 熟悉Linux开发环境 实验报告

    参见http://www.cnblogs.com/lhc-java/p/4970269.html

  9. Sprint10

    进展:设置事件提醒部分已经完成,接下来是实现完成后在添加主界面显示已添加的事件及时间,并可设置可用与不可用.

  10. 增加ubuntu的内存——设置Swap增加内存

    1.查看一下当前Swap分区的状态: $cat /proc/meminfo SwapTotal: 0 kB SwapFree: 0 kB 如果上面二项目都为0,说明没有Swap分区:如果不为0,则说明 ...