Leetcode 15
//用类似双指针的方法,确定第一个i的位置后,j和k向左向右移动使nums[j]+nums[k] = -nums[i];注意特判
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
if (nums.empty()||nums.size() < ) return {};
set<vector<int>> res;
sort(nums.begin(),nums.end());
for(int i=;i < nums.size()-;i++){
for(int j=i+,k=nums.size()-;j < k;){
int sum = nums[j] + nums[k];
if(sum < (-nums[i])){j++;}
else if(sum > (-nums[i])){k--;}
else{
vector<int> cnt;
cnt.push_back(nums[i]);
cnt.push_back(nums[j]);
cnt.push_back(nums[k]);
res.insert(cnt);
j++;k--;
}
}
}
return vector<vector<int>>(res.begin(),res.end());
}
};
Leetcode 15的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- [LeetCode] 15. 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 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 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...
- LeetCode 15. 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——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- LeetCode(15)题解--3Sum
https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- Leetcode 15. Sum(二分或者暴力或者哈希都可以)
15. 3Sum Medium Given an array nums of n integers, are there elements a, b, c in nums such that a + ...
随机推荐
- __devexit_p的功能
看驱动的时候,时常会有如下代码: .remove = __devexit_p(XX_exit), 这里的__devexit_p有什么作用呢? 我在include/linux/init.h中找到了它的定 ...
- 以太坊geth主网全节点部署
以太坊geth主网全节点部署 #环境 ubuntu 16.4 #硬盘500GB(目前占用200G) #客户端安装 # 查看下载页面最新版 # https://ethereum.github.io/go ...
- hive引入jar包--HIVE.AUX.JARS.PATH和hive.aux.jars.path
hive需要引入包时?该怎么引入? 一.hive-site.xml中的hive.aux.jars.path 此配置项对于hive server有效,但是是不会作用到hive shell.也就是说即使你 ...
- 人人网张铁安:Feed系统架构分析(转)
原文:http://www.csdn.net/article/2010-07-26/277273 继成功举办首期TUP活动后,日前在北京丽亭华苑酒店鸿运二厅,由CSDN和<程序员> 杂志联 ...
- Mysql数据库安装与配置
先介绍下dql,dml,ddl,dcl: 安装: 加上这一行: 接下来把mysql设置成随系统自动启动: 开放3306端口(如果是阿里云服务器需要修改安全规则而不是如下): 或者 这里注意,有时候没有 ...
- 本地项目提交到github或者gitlab
在 gitlab中创建新项目 创建好之后会生成一个.Git路径 切换回本地工程文件目录 右键 点击git bash 在git bash 里面执行git init 初始化 git add . 提交当前 ...
- SimpleDateFormat实现String与Date之间的转换
基本用法: java.text.SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); java.util.Date ...
- YTD易出现断层问题,请注意!
declare @table table( company_id int ,--公司编号 quarter_num ),--季度 disti ),--分销商 num int --数量 ) insert ...
- Linux下安装系统清理软件 BleachBit 1.4
sudo add-apt-repository ppa:n-muench/programs-ppasudo apt-get updatesudo apt-get install bleachbit
- zw版【转发·台湾nvp系列Delphi例程】HALCON TestSubsetRegio
zw版[转发·台湾nvp系列Delphi例程]HALCON TestSubsetRegio procedure TForm1.Button1Click(Sender: TObject);var rg0 ...