[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 ...
随机推荐
- Elasticsearch之marvel(集群管理、监控)插件安装之后的浏览详解
前提 Elasticsearch之插件介绍及安装 https://i.cnblogs.com/posts?categoryid=950999&page=2 (强烈建议,从头开始看) 比如,我 ...
- 基于 Web 的 Go 语言 IDE - Wide 1.1.0 发布!
发布 1.1.0 这个版本改进了很多细节,已经完全可以用于正式项目的开发 同时我们上线了 Wide 在线服务 到目前,我们提供了 Wide 和 Solo 两个在线服务,详情请看这里. Wide 是什么 ...
- javafx progressbar
import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans ...
- maven项目引入sqljdbc4 找不到包的完美 解决方案
今天碰到了这个问题,解决了,顺便做一下记录.首先来 重现 一下这个问题,maven install报错,说 找不到这个包,但是其实 我已经安装了. 我们 再来 看看 maven本地仓库里面有 什么,这 ...
- 一个简易版的Angular js 三层 示例
var myApp = angular.module('produceline', []); myApp.factory('ajax', ["$http", "$q&qu ...
- 今日题解------codeforces 895C
题意:给你一个数列,然后找任意数量的数字(除了空集),使得他们的乘机为一个数的平方 我们发现元素最大70,所以我们可以从这里入手,平方数有个性质就是它的所有质因子的指数为偶数 比如:36 = 2*2* ...
- Mblog 部署手册
准备工作 安装 JDK8 安装图片处理工具:GraphicsMagick1.3.20,下载地址 安装 Maven 准备 IDE (如果你不看源码,可以忽略下面的步骤,直接通过Maven编译war包) ...
- 7.Maven之(七)pom.xml配置文件详解
转自:https://blog.csdn.net/qq_33363618/article/details/79438044 setting.xml主要用于配置maven的运行环境等一系列通用的属性,是 ...
- WebService 的Description 属性说明(转)
转自:http://exception.thinksaas.cn/0/173/173623.html 在WebMethod的description 中可使用超文本, 举例: 如上图中,红框类的WebS ...
- MySQL各个版本的区别
文章出自:http://blog.sina.com.cn/s/blog_62b37bfe0101he5t.html 感谢作者的分享 MySQL 的官网下载地址:http://www.mysql. ...