#leetcode刷题之路15-三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
思路:先对vector进行排序,这样的话如果前两个数之和大于9,那么也就可以直接pass了
然后把vector中的数依次放入map中,暴力循环,每次计算前两个数之和,再在map中找到第三个数,存在一个set中,再把set存在一个set中,避免重复。
在不重复的情况下把这三个数放入vector中。
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include <algorithm>
using namespace std; vector<vector<int>> threeSum(vector<int>& nums) {
map<int,int> temp;
vector<vector<int>> ans;
set<set<int>> anss;
int len = nums.size();
int n = ;
sort(nums.begin(), nums.end());
for (int i = ; i < len; i++)
{
temp[nums[i]] = i;
}
for (int i = ; i < len; i++){
for (int j = i+; j < len; j++)
{
int num = -nums[i] - nums[j];
if (num>) continue;
map<int, int> ::iterator add = temp.find(num);
if (add == temp.end() || add->second <= j) continue;
set<int> t;
t.insert(nums[i]);
t.insert(nums[j]);
t.insert(add->first);
//cout << nums[i] << nums[j] << add->first << endl;
if (anss.count(t) == )
{
anss.insert(t);
vector<int> t;
t.push_back(nums[i]);
t.push_back(nums[j]);
t.push_back(add->first);
ans.push_back(t);
cout << nums[i] << nums[j] << add->first << endl;
} }
}
return ans;
} int main(){
vector<int> a = { -, , , , -, - };
vector<vector<int>> ans = threeSum(a);
system("pause");
return ;
}
#leetcode刷题之路15-三数之和的更多相关文章
- LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现
1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(三)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- #leetcode刷题之路9- 回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1:输入: 121输出: true 示例 2:输入: -121输出: false解释: 从左向右读, 为 ...
- leetcode 刷题(1)--- 两数之和
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- Java实现 LeetCode 15 三数之和
15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...
- leecode刷题(8)-- 两数之和
leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输 ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- #leetcode刷题之路16-最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- leetcode题目15.三数之和(中等)
题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...
随机推荐
- OpenLayers介绍和第一个例子(转载)
什么是OpenLayers? 作者:田念明出处:http://www.cnblogs.com/nianming/本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位 ...
- 基于Docker搭建GitLab服务器
运行环境:CentOS 7.4 一.安装Docker Docker安装可以参照runoob教程(http://www.runoob.com/docker/centos-docker-install.h ...
- png的故事:隔行扫描算法
转载自AlloyTeam:http://www.alloyteam.com/2017/06/the-story-of-png-deinterlacing-algorithm/ 前言 前文已经讲解过如何 ...
- nginx https 连接加密
##HTTPS server##server {listen 443;server_name www.shabi.com;ssl on;index index.php index.html index ...
- Atlas+Keepalived实现MySQL读写分离、读负载均衡
一.基础介绍 ========================================================================================== 1. ...
- 关于“为什么不加friend就会提示参数过多”
#include <iostream> using namespace std; class Complex { double real, imag; public: Complex(do ...
- 事件循环进阶:macrotask与microtask
这段参考了参考来源中的第2篇文章(英文版的),(加了下自己的理解重新描述了下), 这里没法给大家演示代码,我就简单说下我的理解吧. promise和settimeout 在一起的时候执行顺序是个有意思 ...
- 【Leetcode】【Medium】Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- C#中的"?"和"??"
摘自:http://www.cnblogs.com/zxjyuan/archive/2009/10/27/1590795.html 如果你看到C#中的“?”问号脑袋里便充满问号,那么这个贴子便是为你而 ...
- webpack学习(五)配置详解
配置详解 //使用插件html-webpack-plugin打包合并html //使用插件extract-text-webpack-plugin打包独立的css //使用UglifyJsPlugin压 ...