[leetcode]三数之和
三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为
[[-1, 0, 1],[-1, -1, 2]]
暴力解超时
思路:选定一个值,两侧逼近求两数和
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
set<vector<int>> res;
if(nums.size()>2)
{
for(int i=0;i<nums.size()-1;i++)
{
if(nums[i]>0)
break;
int j=i+1;
int k=nums.size()-1;
int target=-nums[i];
while(j<k)
{
if(nums[j]+nums[k]==target)
{
vector<int> temp = { nums[i],nums[j],nums[k]};
res.insert(temp);
j++,k--;
}
if(nums[j]+nums[k]>target)
k--;
if(nums[j]+nums[k]<target)
j++;
}
}
}
return vector<vector<int>>(res.begin(),res.end());
}
[leetcode]三数之和的更多相关文章
- LeetCode 三数之和 — 优化解法
LeetCode 三数之和 - 改进解法 题目:给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复 ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode 16. 3Sum Closest. (最接近的三数之和)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- [LeetCode] 923. 3Sum With Multiplicity 三数之和的多种情况
Given an integer array A, and an integer target, return the number of tuples i, j, k such that i &l ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
随机推荐
- docker 导出导入
容器导出 docker export -o myname.tar 容器id 容器导人 docker import myname.tar httpd:v1
- 苹果IPad客户端安装测试软件
背景: 公司在开发一个App应用,需要部署在苹果IPad上进行测试,但是我负责后端开发对安装及测试相关流程不了解.经过一番学习得出以下结论: 1. 首先申请一个苹果的开发账号(一千块左右),大约能注册 ...
- iOS 后台持续定位详解(支持ISO9.0以上)
iOS 后台持续定位详解(支持ISO9.0以上) #import <CoreLocation/CoreLocation.h>并实现CLLocationManagerDelegate 代理, ...
- iOS原生分享功能
iOS_系统原生分享 - CSDN博客 通过UIActivityViewController实现更多分享服务 - 简书 UIActivity - UIKit _ Apple Developer Doc ...
- ABAP术语-Update Module
Update Module 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/20/1114178.html Part of an update ...
- 移动端触摸滑动插件Swiper使用指南
Swiper是一款开源.免费.功能十分强大的移动端内容触摸滑动插件,目前的最新版本是Swiper4.Swiper主要面向的是手机.平板电脑等移动设备,帮助开发者轻松实现触屏焦点图.触屏Tab切换.触屏 ...
- Bootstrap04
Bootstrap04概述 一.输入框组 1.左侧添加文字 <div class="input-group"> <span class="input-g ...
- redis具体使用
key 命名规则:不可包含空格和\n 创建方式: set key value values Strings (Binary-safe strings) Lists Sets Sorted sets ...
- Elasticsearch入门和查询语法分析(ik中文分词)
全文搜索现在已经是很常见的功能了,当然你也可以用mysql加Sphinx实现.但开源的Elasticsearch(简称ES)目前是全文搜索引擎的首选.目前像GitHub.维基百科都使用的是ES,它可以 ...
- 关于C/C++语言的部分BUG
目录 scanf格式匹配引发的错误 局部变量被释放引发的bug 数组写入超出索引维度 指针的指针引发的思考 未定义赋值的变量引发的bug 题外话 scanf格式匹配引发的错误 运行如下程序时,出现 ...