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 ...
随机推荐
- 猜拳 GuessFist
import java.util.Scanner;import java.util.Random;/***跟电脑玩石头剪刀布,需要从控制台输入信息,*然后去判断,然后给予反馈信息*/public cl ...
- C#基础精华----枚举
enums枚举是值类型,数据直接存储在栈中,而不是使用引用和真实数据的隔离方式来存储. (1)默认情况下,枚举中的第一个变量被赋值为0,其他的变量的值按定义的顺序来递增(0,12,3...),因此以下 ...
- idea15 如何设置代码不自动折叠
IDEA默认情况下,如果方法体中只有一行,这个方法体会折叠起来,就像这种
- 1401 - Remember the Word
注意到单词的长度最长100,其实最糟糕复杂度应该能到O(300005*100),需要注意的是在字典树上匹配单词时,一旦不匹配,则后面的就不会匹配,需要break出来(这个害我TLE查了半天,日!),还 ...
- tomcat作为windows服务启动失败解决方法
再使用如下方法注册windows服务时,出现问题: set CATALINA_BASE=E:\tomcat\tomcat-web-server set CATALINA_HOME=E:\tomcat\ ...
- 29 个 PHP 的 Excel 处理类
下面的 PHP Excel 处理类中,包含 Excel 读写.导入导出等相关的类,列表如下: PHP Excel Reader classes 1. Read Excel Spreadsheets u ...
- 07-语言入门-07-A Famous Music Composer
题目地址: http://blog.csdn.net/sevenmit/article/details/8231994 描述 Mr. B is a famous music composer. On ...
- Android编译系统详解(一)
++++++++++++++++++++++++++++++++++++++++++ 本文系本站原创,欢迎转载! 转载请注明出处: http://blog.csdn.net/mr_raptor/art ...
- BZOJ 2005 能量采集(容斥原理)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2005 题意:给定n和m,求 思路:本题主要是解决对于给定的t,有多少对(i,j)满足x= ...
- js中的this怎么理解
本博客供自己学习备忘, js中的this感觉很混乱,目前还有不少地方搞得不是很清楚,看到一篇不错的文章,先摘下来 this是Javascript语言的一个关键字它代表函数运行时,自动生成的一个内部对象 ...