leetcode—3sum
1.题目描述
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)
2.解法分析
之前做过3sum closest的题目,很显然,那里的思路应用到这里是绝对可行的, 但是这个题目我觉得可以用hashmap来做,结果就写了个基于hash的程序,可是结果总是差点,检查了好半天没检查出来,先记录一下
class Solution {public:vector<vector<int> > threeSum(vector<int> &num) {// Start typing your C/C++ solution below// DO NOT write int main() functionvector<vector<int> > result;int numSize=num.size();if(numSize<2)return result;sort(num.begin(),num.end());unordered_multiset<int> myHash;for(int i =0;i<num.size();++i){myHash.insert(num[i]);}int thirdNum=0;vector<int>cur;cur.assign(3,1);for(int i=0;i<num.size()-2;++i){if(i>0&&num[i-1]==num[i])break;if(num[i]>0)break;if(num[i]!=num[i+1]&&myHash.count(num[i])>0)myHash.erase(num[i]);for(int j=i+1;j<num.size()-1;++j){thirdNum=0-num[i]-num[j];if(thirdNum<num[j])break;if(num[j+1]!=num[j])myHash.erase(num[j]);if(myHash.count(thirdNum)>0){if(cur[0]!=num[i]||cur[1]!=num[j]||cur[2]!=thirdNum){cur[0]=num[i];cur[1]=num[j];cur[2]=thirdNum;result.push_back(cur);}}}}return result;}};
leetcode—3sum的更多相关文章
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 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 Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- leetcode — 3sum
import java.util.*; /** * Source : https://oj.leetcode.com/problems/3sum/ * * Created by lverpeng on ...
- LeetCode: 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, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- Leetcode 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- Leetcode 3Sum Closet
二手和3Sum像几乎相同的想法.二进制搜索.关键修剪.但是,在修剪做出很多错误. 然后还有一个更加速了原来的想法O(n^2). #include<iostream> #include &l ...
随机推荐
- java:I/O流
I/O是input/output的缩写,即输入输出端口. 从 文件.键盘.网络 等输入到java程序,再从java程序输出到 文件.显示器.网络等 分类: 1.输入流 和 输出流2.字节流 和 字符流 ...
- OpenCV源码阅读(3)---matx.h---学习心得
在.h文件里定义类,可以通过内联函数的方法完成类基础函数的实现,这样就不需要额外写.cpp文件来写类的内容. 对于操作符重载,可以使用返回应用的方式减小内存开销 _Tp& someclass: ...
- HDU 4358 Boring counting 树状数组+思路
研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...
- scrapy wiki资料汇总
See also: Scrapy homepage, Official documentation, Scrapy snippets on Snipplr Getting started If you ...
- Enabling HierarchyViewer on Rooted Android Devices
转自http://blog.apkudo.com/2012/07/26/enabling-hierarchyviewer-on-rooted-android-devices/. The Hierarc ...
- LA 4127 - The Sky is the Limit (离散化 扫描线 几何模板)
题目链接 非原创 原创地址:http://blog.csdn.net/jingqi814/article/details/26117241 题意:输入n座山的信息(山的横坐标,高度,山底宽度),计算他 ...
- C#路径/文件/目录/I/O常见操作汇总<转载>
文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...
- UVa 11526 H(n)
题意: long long H(int n){ long long res = 0; for( int i = 1; i <= n; i=i+1 ){ res = (res + n/i); } ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- Xcode5 编译ffmpeg,arm64版本;H264
编译选项:./configure —-cc=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchai ...