leetcode15—3Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
想法:对数组进行排序。固定一个位置,然后使用两个指针,一个执行固定点后一个位置,另外一个指向尾端元素。然后判断三个位置的元素和是否为0.如果为0
添加到vector中,如果和大于0,指向尾端元素的指针执行自减操作。如何和小于0,指向固定点位置后一个元素的指针执行自增操作.
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int len = nums.size();
vector<vector<int>> result;
== len)
return result;
sort(nums.begin(),nums.end());
; i < len ; i++){
;
;
&& nums.at(i) == nums.at(i-))
continue;//跳过重复情况
while(front < end ){
){
result.push_back({nums.at(i),nums.at(front),nums.at(end)});
front++;
end--;
))
front++;//避免front对应的值重复
))
end--;//避免end对应的值重复
}){
front++;
}else{
end--;
}
}
}
return result;
}
};
leetcode15—3Sum的更多相关文章
- LeetCode15 3Sum
题意: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- LeetCode15——3Sum
数组中找三个数和为0的结果集 1 // 解法一:先排序 然后固定一个值 然后用求两个数的和的方式 public static List<List<Integer>> three ...
- Leetcode15.3Sum三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- ARTS第六周
第六周.后期补完,太忙了. 1.Algorithm:每周至少做一个 leetcode 的算法题2.Review:阅读并点评至少一篇英文技术文章3.Tip:学习至少一个技术技巧4.Share:分享一篇有 ...
- [Swift]LeetCode15. 三数之和 | 3Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- 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 ...
- 3Sum algorithm - 非常容易理解的实现 (java)
原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
随机推荐
- Points on Cycle (hdu1700,几何)
Points on Cycle Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Java面试题—初级(6)
71,谈谈你对Struts的理解. 1. struts是一个按MVC模式设计的Web层框架,其实它就是一个Servlet,这个Servlet名为ActionServlet,或是ActionServle ...
- HDU4725(KB4-P SPFA+LLL+SLF优化)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- javascript:this指向
this常见指向问题 this的用法 1.直接在函数中使用 谁调用这个函数this就指向谁 2.对象中使用, 一般情况下指向该对象 3.在构造函数中使用 改变this的指向,两种方法的作用都是相同的, ...
- 【读书笔记】iOS-网络-测试与操纵网络流量
一,观测网络流量. 观测网络流量的行为叫做嗅探或数据包分析. 1,嗅探硬件. 从iOS模拟器捕获数据包不需要做特别的硬件或网络配置.如果需要捕获这些数据包,那么可以使用嗅探软件来监听回送设备或是用于连 ...
- mongose + express 写REST API
一.准备工具 先确保电脑已经安装好nodejs 1.mongoose:安装非常简单: npm install mongoose --save [mongoose封装了mongodb的方法,调用mo ...
- [基础架构]PeopleSoft Web Server 重要文件说明
我们都知道PeopleSoft是由几个不同的服务组成的,他们在PeopleSoft体系结构中扮演着自己的角色.这些服务具有不同的文件结构并包含重要的可执行文件和配置文件. 以下是Peoplesoft体 ...
- 配置方法数超过 64K 的应用
随着 Android 平台的持续成长,Android 应用的大小也在增加.当您的应用及其引用的库达到特定大小时,您会遇到构建错误,指明您的应用已达到 Android 应用构建架构的极限.早期版本的构建 ...
- Ubuntu 安装hive + mysql
先安装mysql sudo apt-get update sudo apt-get install mysql-server sudo mysql_secure_installation具体详情请另查 ...
- Sql 中存储过程详细案例
转自:http://www.cnblogs.com/yank/p/4235609.html 概念 存储过程(Stored Procedure):已预编译为一个可执行过程的一个或多个SQL语句. 创建存 ...