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 ...
随机推荐
- Android:Android SDK Manager顺利下载
默认的Android SDK只有Android 4.4的版本,如果需要其他版本的模拟器,需要Android SDK Manager下载, 1.打开Eclipse 2.选择Android SDK Man ...
- python mysql 简单总结(MySQLdb模块 需另外下载)
python 通过DB-API规范了它所支持的不同的数据库,使得不同的数据库可以使用统一的接口来访问和操作. 满足DB-API规范的的模块必须提供以下属性: 属性名 描述 apilevel DB-AP ...
- get Status canceled 请求被取消
1.chrome浏览器下状况: 2.环境: 一个页面A下 包含一个 iframe ,在子页面中用js点击A页面下的链接替换iframe内容脚本如下: window.parent.document.ge ...
- zlib代码生成
1.主页下载zlib-1.2.8的source code的压缩包:F:\Develop Tools\zlib-1.2.8 2.下载安装cmake-2.8.1-win32-x86 3.用cmake生成z ...
- Error Curves(2010成都现场赛题)
F - Error Curves Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- POJ 3080 (字符串水题) Blue Jeans
题意: 找出这些串中最长的公共子串(长度≥3),如果长度相同输出字典序最小的那个. 分析: 用库函数strstr直接查找就好了,用KMP反而是杀鸡用牛刀. #include <cstdio> ...
- windows安装TortoiseGit详细使用教程【基础篇】
标签:tortoisegit 环境:win8.1 64bit 安装准备: 首先你得安装windows下的git msysgit1.9.5 安装版本控制器客户端tortoisegit tortoise ...
- iphone 如何清空UIWebView的缓存
iphonecachingapplicationcookiescacheperformance I actually think it may retain cached information ...
- vs2013编译boost库
打开vs2013>>visual studio tools>>VS2013 x64 本机工具命令提示 cd D:\lib\boost_1_55_0\boost_1_55_0 b ...
- HDU 5303 Delicious Apples 美味苹果 (DP)
题意: 给一个长为L的环,起点在12点钟位置,其他位置上有一些苹果,每次带着一个能装k个苹果的篮子从起点出发去摘苹果,要将全部苹果运到起点需要走多少米? 思路: 无论哪处地方,只要苹果数超过k个,那么 ...