LeetCode(15)题解--3Sum
https://leetcode.com/problems/3sum/
题目:
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:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- 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)
方法一:
两层循环+二分查找,复杂度O(n^2 logn). 太慢了
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
set<vector<int>> res;
vector<vector<int>> output;
vector<int> sol;
sort(nums.begin(),nums.end()); //sort first
int i,j,k,t,x,n=nums.size();
for(i=;i<n;i++){
for(j=i+;j<n-;j++){
t=nums[i]+nums[j];
x=findSol(-t,nums,j+,n-);
if(x!=-){
sol.clear();
sol.push_back(nums[i]);
sol.push_back(nums[j]);
sol.push_back(nums[x]);
res.insert(sol);
}
}
}
set<vector<int>> :: iterator iter;
for(iter=res.begin();iter!=res.end();iter++){
output.push_back(*iter);
}
return output;
}
int findSol(int target,vector<int> nums,int begin,int end){
if(nums[begin]==target)
return begin;
if(nums[end]==target)
return end;
if(nums[begin]>target||nums[end]<target)
return -;
int mid;
while(begin<=end){
mid=(begin+end)/;
if(nums[mid]==target)
return mid;
else if(nums[mid]>target){
end=mid-;
}
else
begin=mid+;
}
return -;
}
};
方法二: 一层循环加two sum思想(https://leetcode.com/problems/two-sum/),O(n^2).
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(),nums.end());
vector<vector<int>> res;
int i,t,a,b,k,n=nums.size();
for(i=;i<n-;i++){
t=-nums[i];
a=i+;
b=n-;
while(a<b){
k=nums[a]+nums[b];
if(k<t){
a++;
}
else if(k>t){
b--;
}
else{
vector<int> sol(,);
sol[]=nums[i];
sol[]=nums[a];
sol[]=nums[b];
res.push_back(sol);
while (a < b && nums[a] == sol[])
a++;
while (a < b && nums[b] == sol[])
b--;
}
}
while (i + < nums.size() && nums[i + ] == nums[i])
i++;
}
//deduplicate, but it is so slow!
//sort(res.begin(), res.end());
//res.erase(unique(res.begin(), res.end()), res.end());
return res;
}
};
LeetCode(15)题解--3Sum的更多相关文章
- LeetCode(16)题解--3Sum Closest
https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...
- [LeetCode][Python]15:3Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 15: 3Sumhttps://oj.leetcode.com/problem ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- LeetCode——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- [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)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- Java 学习(2):java 基础概念
Java作为一种面向对象语言.支持以下基本概念: 多态 继承 封装 抽象 类 对象 实例 方法 重载 基础语法: 一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.以 ...
- LOOPS(hdu 3853)
题意:迷宫是一个R*C的布局,每个格子中给出停留在原地,往右走一个,往下走一格的概率,起点在(1,1),终点在(R,C),每走一格消耗两点能量,求出最后所需要的能量期望 /* 刚开始以为这就是个**题 ...
- Mac 下安装Ant
转自:http://blog.csdn.net/crazybigfish/article/details/18215439 如果你不知道什么是ant,请不要浪费你的时间继续读下去了.或者你对ant是什 ...
- 制作不随浏览器滚动的DIV-带关闭按钮
制作不随浏览器滚动的DIV 效果见 http://bbs.csdn.net/topics/90292438 的滚动效果. $(function(){ //获取要定位元素距离浏览器顶部的距离 var ...
- ExcelHelper类
/// <summary> /// ExcelHelper类 /// </summary> using System; using System.IO; using Syste ...
- LeetCode OJ-- Spiral Matrix II
https://oj.leetcode.com/problems/spiral-matrix-ii/ 螺旋矩阵,和题目一一样的思路,这个是产生n*n 矩阵. #include <iostream ...
- Image与Base64String的互转换
public Image Base64ToImage(string base64String) { // Convert Base64 String to byte[] byte[] imageByt ...
- codevs——2548 自然数积分解
2548 自然数积分解 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 把自然数N分解为若干个自然数之积,输出 ...
- css查缺补漏1
css可以写在哪里 1.和要装饰的标签写在一起 2.内部样式表(内嵌式)是写在head头部标签中,并且用style标签定义 3.外部样式表(外链式) <head><link rel= ...
- Android-TextView属性ellipsize多行失效的解决思路
多余文字显示省略号的常规做法 android:ellipsize="end" //省略号显示在末尾 android:ellipsize="middle" //省 ...