[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 unique triplets in the array which gives the sum of zero.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
问题:求数组中,所有和为0 的三个元素。
解题思路:
自己想了一个解法,但是扔上去超时了,然后在网上看到使用双指针的算法,理解后,把这道题解决了。
第一步,对数组排序。
第二步,
分析1:对于元素 S[i] , 最后的答案可以分为两种,包含 S[i] 的,和不包含 S[i] 的。当包含 S[i] 的情况都找到后,后续就可以不用在考虑 S[i] 。
对于 S[i] , l = i+1, r = len-1 。若 s[i] + S[l] + S[r] == 0, 则为原问题的一个解。
- 当 S[i] + S[l] > -S[r] 时,则 r--
- 当 S[i] + S[l] < -S[r] 时,则 l++
- 当 S[i] + S[i] = -S[r] 时, 表示是原问题的一个解,则 l++, r--;
第三步,性能优化。同样根据分析1,若 S[i] == S[i+1],可以跳过。
vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> res; std::sort(nums.begin(), nums.end()); map<string, vector<int>> key_res; for (int i = ; i < (int)nums.size(); i++) { // star 剪枝优化
if (i >= ) {
while (nums[i] == nums[i-]) {
i++;
continue;
}
}
// end 剪枝优化 int l = i+;
int r = (int)nums.size()-; while (l < r) {
if (nums[l] + nums[r] > -nums[i]) {
r--;
continue;
}
else if (nums[l] + nums[r] < -nums[i]){
l++;
}else{
string k = to_string(nums[i]) + "," + to_string(nums[l]) + "," + to_string(nums[r]); vector<int> tmp = {nums[i], nums[l] , nums[r]};
key_res[k] = tmp; l++;
r--;
}
}
} map<string, vector<int>>::iterator m_iter;
for (m_iter = key_res.begin(); m_iter != key_res.end(); m_iter++) {
res.push_back(m_iter->second);
} return res;
}
[LeetCode] 3Sum 解题思路的更多相关文章
- leetcode array解题思路
Array *532. K-diff Pairs in an Array 方案一:暴力搜索, N平方的时间复杂度,空间复杂度N 数组长度为10000,使用O(N平方)的解法担心TLE,不建议使用,尽管 ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- leetcode BFS解题思路
Word Ladder 思路一:单向bfs, 使用visited数组记录哪些已经访问过了, 访问过的就不允许再次入队, 同时这里想到的是使用26个英文字母,枚举可能的取值, 类似brute force ...
- [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] 76. Minimum Window Substring 解题思路
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LeetCode] Word Break 解题思路
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] 134. Gas Station 解题思路
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
随机推荐
- MYSQL命令cmd操作
今天我们就来看一下数据库的各种命令,以下命令全部是从CMD命令窗口下的命令行输入指令,首先如果如果输入mysql,系统提示“mysql不是内部命 令或外部命令.那么这其实是环境变量没有设置好的原因,例 ...
- Fedora 21 安装QQ国际版
首先安装依赖包 sudo yum install freetype.i686 libpng.i686 libgcc.i686 libXau.i686 点击下载wine-2012qq国际版 unzip ...
- Linux Shell脚本之自动修改IP
作为一名Linux SA,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者MAC绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名.ip信息.网关等配置.修改成特定的主机名 ...
- jQuery插件综合应用(四)头像设置
一.操作流程 会员点击头像设置,弹出一个层,在层中,有上传图片的按钮,用户点击按钮上传图片,图片在服务器端按大小压缩保存(方便剪切).保存后,在前端显示,然后用户可修剪图片.选择图片区域,点击提交,保 ...
- jquery 的新使用用法
在1.9.1jquery版本中,live 被替换了,现在使用on事件 在动态添加的行中使用下面可响应 $("tbody").on("click","b ...
- VS2013发布web项目到IIS上遇到的问题总结
vs2010发布网站到本地IIS的步骤 http://blog.csdn.net/cx_wzp/article/details/8805365 问题一:HTTP 错误 403.14 - Forbid ...
- STM32学习内容和计划
一.STM32学习内容(流程) 1.学习STM32开发流程 ①MDK使用.建立工程.调试等 ②库开发方法 2.学习STM32常用外设开发 ①GPIO ②中断 ③定时器 ④串口 ⑤CAN 3.学习STM ...
- android SurfaceView绘制 重新学习--基础绘制
自从大二写了个android游戏去参加比赛,之后就一直写应用,一直没用过SurfaceView了,现在进入了游戏公司,准备从基础开始重新快速的学一下这个,然后再去研究openGL和游戏引擎. 直接上代 ...
- cmd下运行java文件时,找不到或无法加载主类的解决方法
最近再看java,却被一个看似很基础的hellorworld头疼了十几分钟,百度了一下,若你在cmd下编辑及运行.java文件时报错,解决方案如下,如图所示:
- Java中double类型的数据精确到小数点后两位
Java中double类型的数据精确到小数点后两位 多余位四舍五入,四种方法 一: double f = 111231.5585;BigDecimal b = new BigDecimal(f); d ...