【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 ...
随机推荐
- Moccakids-Tangram Puzzle 限免啦!
圣诞前上线的App,Tangram Puzzle 限免,大家去下载玩玩. 介绍网站:http://www.mokamisu.com/ App Store:https://itunes.apple.co ...
- ie8下jquery改变PNG的opacity出现黑边,ie6下png透明解决办法
目前互联网对于网页效果要求越来越高,不可避免的用到PNG图片,PNG分为几种格 式,PNG8 PNG24 PNG32,其中最常用的,也是显示效果和大小比较适中的则是PNG24,支持半透明,透明,颜色也 ...
- Intellij IDEA 配置Subversion插件
在使用Intellij的过程中,突然发现svn不起效了,在VCS–>Checkout from Version Control中也未发现Subversion这一项.如下图: 一.原因查找 经过分 ...
- asp 时间倒数后按钮可用
<asp:Button runat="server" ID="btn" Text="免费获取验证码" onclick="bt ...
- 正则指引-量词demo
class Program { static void Main(string[] args) { string str = "1\"3"; var re1 = Rege ...
- 安装mmseg出错 config.status: error: cannot find input file: src/Makefile.in
aclocallibtoolize --forceautomake --add-missingautoconfautoheadermake clean
- iOS AVKit音视频播放全面详解
公司项目中经常要用到音视频处理,也需要去定制一些东西,然后整理这些音视频处理就显得尤为重要!方便自己和广大朋友学习收藏! 以下参考连接特别重要: 苹果官方:AVKit API 苹果官方:AVFound ...
- Castle.ActiveRecord (V3.0.0.130)
为项目添加 Castle.ActiveRecord 的引用: 安装成功后,查看项目的引用如图: 配置文件 App.Config (MySQL) <?xml version="1.0&q ...
- CISA 信息系统审计知识点 [第一章. 信息系统审计过程 ]
对有志成为审计师或者IT管理者de朋友, 第一章. 信息系统审计过程 1. IS 审计和保障标准.指南.工具.职业道德规范 信息技术保证框架(ITAF,Information Technology A ...
- WPF打印票据
最近工作的内容是有关于WPF的,整体开发没有什么难度,主要是在打印上因为没有任何经验,犯了一些难,不过还好,解决起来也不是很费劲. WPF打印票据或者是打印普通纸张区别不大,只是说打印票据要把需要打的 ...