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 ...
随机推荐
- 2014-9-17二班----10 web project
package cn.rwkj.servlet; import java.io.IOException; import javax.servlet.ServletException; import j ...
- Android 多线程断点下载
package com.itheima.mutiledownloader; import java.io.BufferedReader; import java.io.File; import jav ...
- px,dp,sp单位转换工具类
在layout中使用dp 在代码中getWidth系列得到的是px 设置字体大小时使用的是sp /** * Android大小单位转换工具类 */ public class PxDpSpUtil { ...
- JAVA的核心概念:接口(interface)
JAVA的核心概念:接口(interface) 接口与类属于同一层次,实际上,接口是一种特殊的抽象类. 如: interface IA{ } public interface: 公开接口 与 ...
- Java 比较两张图片的相似度
import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; /** * 比较两张图片 ...
- iOS开发:视图生命周期
iOS应用的视图状态分为以下几种 在viewcontroller的父类UIViewController中可以看到如下代码,通过重写不同的方法对操作视图渲染. @available(iOS 2.0, * ...
- powerScript脚本
一.powerScript的语法 1.0变量的命名及使用 powerscript的标识符(变量名称)必须以字母或下划线开头,其它的字符可以是下划线(_).短横线(-).美元符号($).号码符号(#) ...
- LeetCode: 3SumClosest
Title : Given an array S of n integers, find three integers in S such that the sum is closest to a g ...
- String.IsNullOrEmpty 方法
参数 value:一个String引用 返回值 如果 value 参数为 空引用(在 Visual Basic 中为 Nothing) 或空字符串 (""),则为 true:否则为 ...
- why dicePlayer cannot player with defy mb526
硬件加速视频播放器 DicePlayer v2.0.38 ... ..... ...... ........ \ 局限性:- 视频兼容性依赖于您设备的视频硬解码能力