[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 ...
随机推荐
- C++的继承和Java继承的比较
在C++中继承可分为公有继承(public)保护继承(protected)和私有继承(private),而在Java中默认只有一种继承(相当于C++中的公有继承)下面我们来看一段代码 #include ...
- 转 C#:使用MD5对用户密码加密与解密
C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1 ...
- 关于 js 的框架方向
关于 js 的框架方向 http://www.breck-mckye.com/blog/2014/12/the-state-of-javascript-in-2015/?utm_source=ourj ...
- 今日SGU 5.2
SGU123 题意:求和 收获:无 #include<bits/stdc++.h> #define de(x) cout<<#x<<"="< ...
- Font-Awesome最新版完整使用教程
何为Font-Awesome Font Awesome gives you scalable vector icons that can instantly be customized - size, ...
- VmWare 与 宿主主机通信 STEP BY STEP (适用于刚開始学习的人)
基本原理 在虚拟机中有三种通信方式,例如以下图所看到的 1. Bridged(桥接模式) 在桥接模式下,VMware虚拟出来的操作系统就像是局域网中的一独立的主机,它能够訪问网内不论什么一台机器只是你 ...
- 38..Node.js工具模块---底层的网络通信--Net模块
转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js Net 模块提供了一些用于底层的网络通信的小工具,包含了创建服务器/ ...
- web.xml的配置及加载顺序
一web.xml加载过程(步骤): 1.启动WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> ...
- HDU 4927 Series 1(推理+大数)
HDU 4927 Series 1 题目链接 题意:给定一个序列,要求不断求差值序列.直到剩一个,输出这个数字 思路:因为有高精度一步.所以要推理一下公式,事实上纸上模拟一下非常easy推出公式就是一 ...
- layout-maxWidth属性用法
对于maxWidth属性,相信大家都不陌生.不过,今天我遇到了一个问题,就是当我希望一个relayout的宽度有个最大值的时候,用maxWidth却没办法实现.这里总结下maxWidth 的用法 1. ...