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. 深入理解java:2.3.1. 并发编程concurrent包 之Atomic原子操作(循环CAS)

    java中,可能有一些场景,操作非常简单,但是容易存在并发问题,比如i++, 此时,如果依赖锁机制,可能带来性能损耗等问题, 于是,如何更加简单的实现原子性操作,就成为java中需要面对的一个问题. ...

  2. 【查阅】mysql配置文件/参数文件重要参数笔录(my.cnf)

    持续更新,积累自己对参数的理解 [1]my.cnf参数 [client]port = 3306socket = /mysql/data/3306/mysql.sockdefault-character ...

  3. 洛谷 P5660 数字游戏 & [NOIP2019普及组]

    传送门 洛谷改域名了QAQ 解题思路 没什么好说的,一道红题,本不想发这篇博客 ,但还是尊重一下CCF吧QAQ,怎么说也是第一年CSP呢! 用getchar一个个读入.判断.累加,最后输出即可. 不过 ...

  4. 不能将X*类型的值分配到X*类型的实体问题的解决方法

    今天在学习链表的过程中遇到了这个问题,我用如下方法定义了一个结构体,然后这个函数想要在链表头插入一个节点.但是在函数的最后一行却出现了报错:不能将MyLinkedList * 类型的值分配到MyLin ...

  5. 【转】Hadoop 1.x中fsimage和edits合并实现

    在NameNode运行期间,HDFS的所有更新操作都是直接写到edits中,久而久之edits文件将会变得很大:虽然这对NameNode运行时候是没有什么影响的,但是我们知道当NameNode重启的时 ...

  6. 利用中转输出表制作HijackDll

    [原创]利用中转输出表制作HijackDll(附工具源码)作 者: baixinye时 间: 2012-08-05,16:48:45链 接: http://bbs.pediy.com/showthre ...

  7. time和datetime的区别

    time在 Python 文档里,time是归类在Generic Operating System Services中,换句话说, 它提供的功能是更加接近于操作系统层面的.通读文档可知,time 模块 ...

  8. Set中如何区分重复元素

    Set接口常用实现类:HashSet和TreeSet HashSet区分重复元素: 先使用hashcode方法判断已经存在HashSet中元素的hashcode值和将要加入元素hashcode值是否相 ...

  9. 简单的物流项目实战,WPF的MVVM设计模式(三)

    往Services文件里面添加接口以及实现接口 IUserService接口 List<User> GetAllUser(); GetUserService类 ConnectToDatab ...

  10. GIT服务器项目部署和自动同步

    1.1.初始化Git仓库首先我们选定一个目录作为Git仓库,假定是/home/data/share/share.git,在/home/data/目录下输入命令: $ cd /home/data/ $ ...