#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 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...
随机推荐
- Sencha Architect打开闪退问题修复
删除以下位置的cache文件夹 C:\Users\Administrator\AppData\Local\Sencha\Sencha Architect 3.2\Cache bug解决参考 https ...
- 类型同时存在于A.dll和B.dll中的解决办法
引用B.dll后,右键引用中的B.dll属性->别名->Test using的最前面加上 extern alias Test; 使用 Test.MyClass.DoSth();
- Web前端面试指导(十五):CSS样式-display有哪些作用?
题目点评 其实就是要你说清楚该属性有哪些值,每个值都有什么作用,这个题目可以答得很简单,但要答全也并非是一件容易的事情. 元素默认的display值的情况如下(这个一般很少人注意这一点) block( ...
- Spring 框架(一)
1 spring框架概述 1.1 什么是spring l Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert ...
- Java Struts2 (二)
二.封装请求正文到对象中(非常重要) 1.静态参数封装 在struts.xml配置文件中,给动作类注入值.调用的是setter方法. 原因:是由一个staticParams的拦截器完成注入的. 2.动 ...
- 关联函数 map 的基本用法
1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...
- c++开发ocx入门实践一
原文:http://blog.csdn.net/yhhyhhyhhyhh/article/details/51374200 最近项目中利用ocx封装了底层视频播放及处理的控件,以供c#和web调用.对 ...
- 九、background及相关所有属性
先看看如下所示的视效图应该如何显示背景阴影? #header { height: 180px; background: url(../images./bg.png) no-repeat center ...
- css tips: 清除float影响,containing的div跟随floated sub等
/** * For modern browsers * 1. The space content is one way to avoid an Opera bug when the * content ...
- Session跨域、Session共享、Mode=StateSever方式解决问题
前言 很多童鞋在工作或面试的过程中,也许会遇到这样的问题,使用Session,怎样让多个站点实现Session共享的问题,也就是在A站点登录,那么在B站点就不需要重新登录了那?如果采用Session保 ...