[array] leetCode-15. 3Sum-Medium
leetCode-15. 3Sum-Medium
descrition
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]
]
解析
方法1
3 重循环,时间复杂度-O(n^3),空间复杂度-O(1)
for(int i=0; i<array.size(); i++){
for(int j=i+1; j<array.size(); j++){
for(int k=j+1; k<array.size(); k++){
// 检查是否合法
}
}
}
方法2
思考方向:是否可以减少方法 1 中的循环查找次数??
时间复杂度-O(n^2),空间复杂度-O(1)。
对数组进行排序,需要时间 O(nlog(n))。使用两重循环,最外层循环 a=array[i],内层循环使用双向指针进行遍历,b 从左到右,c 从右到左,思想和 two sum 一样。(参看代码)
code
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
class Solution{
public:
vector<vector<int> > threeSum(vector<int>& nums){
// The solution set must not contain duplicate triplets.
vector<vector<int> > ans;
sort(nums.begin(), nums.end()); // ascending
for(int i=0; i<nums.size(); i++){
int target = -nums[i];
int ileft = i+1;
int iright = nums.size()-1;
while(ileft < iright){
int sum = nums[ileft]+nums[iright];
if( sum == target){
// answer
vector<int> temp(3);
temp[0] = nums[i];
temp[1] = nums[ileft];
temp[2] = nums[iright];
ans.push_back(temp);
// skip duplicate
while(ileft<iright && nums[ileft] == temp[1])
ileft++;
while(ileft<iright && nums[iright] == temp[2])
iright--;
}else if (sum < target){
ileft++;
}else{
// sum > target
iright--;
}
}
// skip duplicate
while((i+1)<nums.size() && nums[i+1] == nums[i])
i++;
// i point to the same value, after the i++ in the for loop, i will point to next value
}
return ans;
}
};
int main()
{
return 0;
}
[array] leetCode-15. 3Sum-Medium的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- 蜗牛慢慢爬 LeetCode 15. 3Sum [Difficulty: Medium]
题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- [LeetCode] 15. 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 ...
- LeetCode 15. 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 ...
- LeetCode——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- leetCode 15. 3Sum (3数之和) 解题思路和方法
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- Leetcode 15. 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 ...
- 【leetcode】3Sum (medium)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
随机推荐
- Impala数据处理(加载和存储)
不多说,直接上干货! Hive与Impala都是构建在Hadoop之上的数据查询工具,那么在实际的应用中,它们是如何加载和存储数据的呢? Hive和Impala存储和加载表,和所有的关系型数据库一样, ...
- 适合ASP.NET Web API使用的场景
富客户端web应用程序:ASP.NET Web API适合大量使用AJAX调用的富客户端应用程序,如Silverlight应用程序,基于Adobe Flash的应用程序或单页应用程序(SPA)等. 本 ...
- SQL、Linq相关字段搜索
结合一些分词组件,如盘古,对于用户查询关键字红按钮很容易分出 ‘红’ ‘按钮’二个单词 我们假设产品名称列里面是红色,规格里面是按钮 /* 普通sql实现全文搜索declare @key1 nvarc ...
- java 参数
-Xmx:size java最大堆内存 -Xms:size 初始化内存 -Xmn:size 年轻带堆大小 -XX:NewSize=size 年轻带的大小 -XX:NewRatio=ratio 年轻带和 ...
- Android Studio配置SVN 以及使用代码管理
一.Android Studio配置SVN Android Studio关联配置SVN非常easy,在Settings里面.找到Version Control->Subversion.在这个页面 ...
- 通过OpenSSL解析X509证书基本项
在之前的文章"通过OpenSSL解码X509证书文件"里.讲述了怎样使用OpenSSL将证书文件解码,得到证书上下文结构体X509的方法. 以下我们接着讲述怎样通过证书上下文结构体 ...
- git -处理分支合并
1.分支间的合并 1)直接合并:把两个分支上的历史轨迹合二为一(就是所以修改都全部合并) zhangshuli@zhangshuli-MS-:~/myGit$ vim merge.txt zhangs ...
- JXNU 新生选拔赛
1001 最小的数 Problem Description 定义一种正整数集合K,集合中有N个数,集合中元素Ki(1<=i<=N)是包含i个不同质因子的最小的数.因为Ki可能会很大,所以将 ...
- Node里面的对象创建问题
1.利用new Object()创建时 var a =new Object() a.b = 1 console.log(a) // 打印出来是[object Object] console.log(J ...
- 【2017百度之星程序设计大赛 - 复赛】Valley Numer
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6148 [题意] 在这里写题意 [题解] 先把1..N里面的山峰数字个数算出来->x 然后用N减去这 ...