【leetcode】3 SUM
3 SUM
原题:
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
题意
找到数组中三个数加起来的值等于0的所有集合。
思路:
2 SUM问题的解决方法是先将数组排序,前后分别一个指针向中间逼近,时间复杂度为O(NlogN)。所以3 SUM的做法可以模仿该做法,从数组中先取一个数t,再去用2 SUM的做法去获得剩下两个加和为-t的数。
有一个最大的问题是结果去重,可以有两个方法解决,首先想到的是用set数据结构,另一种方法是遇到结果相同的数直接跳过。
代码
使用set的方法
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
set<vector<int>> s;
for(int i = 0;i<nums.size();i++)
{
//如果第一个数已经大于0,可以不判断后面的数了,因为三个大于0的数不可能加起来等于0
if(nums[i] > 0) break;
vector<int> ans0(3,0);
ans0[0] = nums[i];
int t = 0 - nums[i];
int l = i+1, r = nums.size()-1;
while(l < r)
{
if(nums[l] + nums[r] == t)
{
ans0[1] = nums[l];
ans0[2] = nums[r];
//如果下一个数等于前一个数,跳过该数
while(l<r && nums[l+1] + nums[r] == t) l++;
while(l<r && nums[l] + nums[r+1] == t) r++;
s.insert(ans0);
l++;
r--;
}else if(nums[l] + nums[r] < t)
l++;
else
r--;
}
}
//用set构造vector
vector<vector<int>> ans(s.begin(), s.end());
return ans;
}
};
不使用set:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
for(int i = 0;i<nums.size();i++)
{
if(nums[i] > 0) break;
//如果下一个数和当前数相等,跳过
if(i>0 && nums[i]==nums[i-1]) continue;
vector<int> ans0(3,0);
ans0[0] = nums[i];
int t = 0 - nums[i];
int l = i+1, r = nums.size()-1;
while(l < r)
{
if(nums[l] + nums[r] == t)
{
ans0[1] = nums[l];
ans0[2] = nums[r];
//如果下一个数和当前数相等,跳过
while(l<r && nums[l+1] + nums[r] == t) l++;
while(l<r && nums[l] + nums[r+1] == t) r++;
ans.push_back(ans0);
l++;
r--;
}else if(nums[l] + nums[r] < t)
l++;
else
r--;
}
}
vector<vector<int>> ans(s.begin(), s.end());
return ans;
}
};
结果
不使用set的做法速度更快一些。
311 / 311 test cases passed.
Status: Accepted
Runtime: 49 ms
311 / 311 test cases passed.
Status: Accepted
Runtime: 109 ms
【leetcode】3 SUM的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【leetcode】Path Sum IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 【LeetCode】Path Sum(路径总和)
这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...
- 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)
这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...
- 【LeetCode】404. Sum of Left Leaves 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
随机推荐
- [推荐]Hadoop+HBase+Zookeeper集群的配置
[推荐]Hadoop+HBase+Zookeeper集群的配置 Hadoop+HBase+Zookeeper集群的配置 http://wenku.baidu.com/view/991258e881c ...
- C# 热敏打印机 Socket 网络链接 打印 图片
C# 热敏打印机 Socket 网络链接 打印 图片 (一) http://www.cnblogs.com/rinack/p/4838211.html C# 热敏打印机 Socket 网络链接 打印 ...
- 【转】Android类动态加载技术
http://www.blogjava.net/zh-weir/archive/2011/10/29/362294.html Android应用开发在一般情况下,常规的开发方式和代码架构就能满足我们的 ...
- [转] Redis系统性介绍
Redis系统性介绍 http://blog.nosqlfan.com/html/3139.html?ref=rediszt 虽然Redis已经很火了,相信还是有很多同学对Redis只是有所听闻或者了 ...
- SSH在Jenkins中的使用
我们今天在迁移Jenkins的时候又出现无法调用私钥来获取oschina的git代码和使用scp拷贝无法验证的问题.我发现主要的问题实际上是关于ssh的问题,因为git和scp都是通过ssh来实现与远 ...
- 深入剖析 redis RDB 持久化策略
简介 redis 持久化 RDB.AOF redis 提供两种持久化方式:RDB 和 AOF.redis 允许两者结合,也允许两者同时关闭. RDB 可以定时备份内存中的数据集.服务器启动的时候,可以 ...
- java后台进程和线程优先级
1. 后台线程:处于后台运行,任务是为其他线程提供服务.也称为“守护线程”或“精灵线程”.JVM的垃圾回收就是典型的后台线程. 特点:若所有的前台线程都死亡,后台线程自动死亡. 设置后台线程:Thre ...
- C# Like参数化 小记
strBuilder.Append(" and b.name like '%' + @name + '%'"); parameters.Add(new SqlParameter(& ...
- ch4 MySQL 安全管理
第 4 章 MySQL 安全管理 前言 对于任何一个企业来说,其数据库系统中所保存数据的安全性无疑是非常重要的,尤其是公司的有些商业数据,可能数据就是公司的根本,失去了数据的安全性,可能就是失去了公司 ...
- RedRabbit——基于BrokerPattern服务器框架
RedRabbit 经典网游服务器架构 该图省略了专门用途的dbserver.guildserver等用于专门功能的server,该架构的优点有: l LoginGate相当于DNS,可以动态的保证G ...