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 + ...
随机推荐
- checkbox的name与JavaBean的交互时发现的一个现象
一个页面: <form action="reg.jsp" method="post"> <ul> <li>算法选择</ ...
- console access jquery--------json
jq = document.createElement('script'); jq.src = "file:///home/liulqiang/jquery.js"; docu ...
- angular js 上传插件 ng-file-upload 使用时注意事项
项目框架为angular js,需要用到文件上传,百度之后先选择了angular-file-upload,githuab上API文档很全,想要具体了解,可以仔细研究一下.在这里简单回顾一下自己使用的插 ...
- SpringCloud 进阶之分布式配置中心(SpringCloud Config)
1. SpringCloud Config SpringCLoud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用 的所有环境提供了一个中心化的外部配置; ...
- mysql 数据操作 单表查询 having 过滤
SELECT 字段1,字段2... FROM 库名.表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 1.首先找到表 库.表 ...
- PHP debug_backtrace() 函数打印调用处的调试信息
http://php.net/manual/zh/function.debug-backtrace.php debug_backtrace (PHP 4 >= 4.3.0, PHP 5, PHP ...
- PHP 自动加载的简单实现(推荐)
基于psr的规范,使用命名空间和spl_autoload_register()来实现自动加载 文件结构: |--Api |--Account.php |--User.php |--Service |- ...
- Linux下编译安装PHP扩展memcached
[安装 libevent] $ tar zxvf libevent-2.0.20-stable.tar.gz $ cd libevent-2.0.20-stable/$ ./configure --p ...
- [sql] 同库表(结构)的备份和sql聚合&navicat使用
同库表的备份-赋值表结构和数据SQL语句 参考 有时候我们处理某个表时,需要先备份下这个表到当前这个库,然后再执行sql. 站在sql角度,就无需在mysqldump或者诸如导出sql的方式来备份了. ...
- PAT 1114 Family Property[并查集][难]
1114 Family Property(25 分) This time, you are supposed to help us collect the data for family-owned ...