15. 3Sum
Medium

Given an array nums of n integers, are there elements abc in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
] 题解: 三个数的和,思路就是求任意两个数的和x,然后O(n^2),再用O(1)的搜索搜索-x是否再数组内就可以了,O(1)搜索用哈希就行,或者用O(longn)的二分的方法也可以接收,我用的自带的find函数,最后也过了
 class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
multiset<int> left;
multiset<int> right;
set<vector<int>> ans;
int fl = ;
for(int i = ;i < nums.size(); i++){
if(nums[i] == ) fl++;
else if(nums[i]<) left.insert(nums[i]);
else if(nums[i]>) right.insert(nums[i]);
}
set<int>:: iterator lit;
set<int>:: iterator rit;
for(lit = left.begin(); lit!=left.end(); lit++){
rit = lit;
rit++;
for(; rit != left.end(); rit++){
int tt = (*lit)+(*rit);
if(right.find(-tt)!=right.end()){
vector<int> New;
New.push_back( (*lit));New.push_back( (*rit));New.push_back( -tt);
ans.insert(New);
}
}
}
for(rit = right.begin(); rit!=right.end(); rit++){
lit = rit;
lit++;
for(; lit != right.end(); lit++){
int tt = (*lit)+(*rit);
if(left.find(-tt)!=left.end()){
vector<int> New;
New.push_back( -tt);New.push_back( (*rit));New.push_back( (*lit));
ans.insert(New);
}
}
}
if(fl>=){
set<int>:: iterator it;
for(it = left.begin(); it!=left.end(); it++){
if(right.find(-(*it))!=right.end()){
vector<int> New;
New.push_back((*it));New.push_back();New.push_back(-(*it));
ans.insert(New);
}
}
}
if(fl>=){
vector<int> New;
New.push_back();New.push_back();New.push_back();
ans.insert(New);
}
vector<vector<int>> finalans;
set<vector<int>>:: iterator i;
for(i = ans.begin(); i!=ans.end(); i++){
vector<int> t = (*i);
finalans.push_back(t);
}
return finalans;
}
};

Leetcode 15. Sum(二分或者暴力或者哈希都可以)的更多相关文章

  1. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  2. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  3. 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

    题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...

  4. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  5. Codeforces Round #402 (Div. 2) D题 【字符串二分答案+暴力】

    D. String Game Little Nastya has a hobby, she likes to remove some letters from word, to obtain anot ...

  6. 【矩阵哈希】【二分答案】【哈希表】bzoj1567 [JSOI2008]Blue Mary的战役地图

    引用题解:http://hzwer.com/5153.html 当然,二分可以换成哈希表. #include<cstdio> #include<iostream> #inclu ...

  7. [Leetcode 15]三数之和 3 Sum

    [题目] Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...

  8. LeetCode#15 | Three Sum 三数之和

    一.题目 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意:答案中不可以包含 ...

  9. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

随机推荐

  1. c#字符串代码,动态创建编译器

    https://www.cnblogs.com/mrma/p/3998679.html 试了,确实可行,在unity也能用 值得注意的是UnityScript.Scripting.Evaluator ...

  2. Git push “fatal: Authentication failed ”

    Git push "fatal: Authentication failed " 问题原因 之前设置了两步验证 If you enabled two-factor authenti ...

  3. 利用Flot作基于时间段的曲线图

    Flot是一个可以用于绘制多种图表的开源的JS库,Flot本身的功能已经是基本可以满足日常的需要啦,更可喜的是Flot还有很多的插件可以使用,从而为我们提供更加强大的定制功能,本文在作图中使用的显示坐 ...

  4. git命令?

    #文件及文件夹创建删除    mkdir  文件名称    (创建文件夹)    touch  文件名称    (创建文件)    rm -r  文件名称     (递归删除)    rm -rf 文 ...

  5. 01.AutoMapper 之约定(Conventions)

    转载(https://www.jianshu.com/p/d4c472d95da4)   约定(Conventions) 条件对象映射器 条件对象映射器根据源类型和目标类型之间的条件生成新类型映射. ...

  6. SQL SERVER 索引维护

    -- 全数据库索引重建 DECLARE @name varchar(100)DECLARE authors_cursor CURSOR FOR Select [name] from sysobject ...

  7. 012-linux系统管理——进程管理与工作管理

    linux系统管理——进程管理 top 命令是使用 top - :: up :, user, load average: 0.06, 0.60, 0.48 #五分钟钱,十分钟前,十五分钟前负载的值根据 ...

  8. VB中RaiseEvent语句的功能及用法

    Creat a new class named Class1, it's codes like this: Public Event MyEvent() Public Sub RaiseTheEven ...

  9. Java并发(具体实例)——几个例子

    一步步优化页面渲染功能                                                           本节将模拟一个简单的页面渲染功能,它的作用是将HTML页面绘 ...

  10. Linux openssh8.0p1升级步骤

    前期准备开启本机telnet服务,以防openssh升级失败无法连接服务器.注:redhat 5 6 和 redhat7 开机启动配置相关文件不同,请注意 1.安装zlibtar -xzvf zlib ...