Leetcode 15. Sum(二分或者暴力或者哈希都可以)
Given an array nums of n integers, are there elements a, b, c 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(二分或者暴力或者哈希都可以)的更多相关文章
- 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 ...
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix
题目传送门 /* 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 */ #include <cstdio> #include < ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- 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 ...
- 【矩阵哈希】【二分答案】【哈希表】bzoj1567 [JSOI2008]Blue Mary的战役地图
引用题解:http://hzwer.com/5153.html 当然,二分可以换成哈希表. #include<cstdio> #include<iostream> #inclu ...
- [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? ...
- LeetCode#15 | Three Sum 三数之和
一.题目 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意:答案中不可以包含 ...
- [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 ...
随机推荐
- cocos2dx基础篇(12) 编辑框之一CCTextFieldTTF
前面我们讲了精灵贴图.标签.菜单.按钮.感觉似乎少了点什么?UI控件里是不是应该还有一个很重要的控件--编辑框.在手机网游中,启动游戏,过了开场动画后,基本上显示的第一个界面应该就是游戏的登录界面了吧 ...
- 学习使用CGI和HTML
目标和需求: (1)通过网页查询并设置开发板的网络参数,要求至少可查询IP地址.子网掩码.网关.MAC地址,可设置自动获取IP或固定IP,设置包括查询的内容 (2)使用CGI编程+HTML实现简单数据 ...
- 005 gcc 的简单使用
0. 前言 本文主要讲关于 gcc 的几种编译方式 不妨设文件名为 test.c 1. 方法一 $ gcc test.c (Windows OS)编译成功的话,没有回馈,在 test.c 所在的文件夹 ...
- [BZOJ 4025]二分图(线段树分治+带边权并查集)
[BZOJ 4025]二分图(线段树分治+带边权并查集) 题面 给出一个n个点m条边的图,每条边会在时间s到t出现,问每个时间的图是否为一个二分图 \(n,m,\max(t_i) \leq 10^5\ ...
- GUI自动化测试中优化测试用例思维方法
1.测试脚本与数据解耦(数据驱动) 让操作相同但是数据不同的测试可以通过同一 套自动化测试脚本来实现,只是在每次测试执行时提供不同的测试输入数据. 2.页面对象模型(POM) 以页面为单位来封装页面上 ...
- django基础知识之认识MVT MVC??
MVT Django是一款python的web开发框架 与MVC有所不同,属于MVT框架 m表示model,负责与数据库交互 v表示view,是核心,负责接收请求.获取数据.返回结果(相当于mvc的c ...
- 安装最新版Elasticsearch报错
1 问题:ERROR: bootstrap checks failed max file descriptors [4096] for elasticsearch process likely too ...
- javascript xml转json
1.代码 //加载xml数据 function loadXml(str) { if (str == null) { return null; } var doc = str; try{ doc = c ...
- json与导入模块目录
import json """主要用于不同语言的数据公用 """ info = {"a":1,"b" ...
- Windows 环境下安装redis 及其PHP Redis扩展
1.安装Redis (1)这里选择在github官网上下载Redis,地址:Redis下载地址 下载压缩包(如下图),并解压到本地目录,我放在D:\redis (2)验证Redis安装是否成功打开命令 ...