Given an array S of n integers, are there elements abc 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 解题思路的更多相关文章

  1. leetcode array解题思路

    Array *532. K-diff Pairs in an Array 方案一:暴力搜索, N平方的时间复杂度,空间复杂度N 数组长度为10000,使用O(N平方)的解法担心TLE,不建议使用,尽管 ...

  2. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  3. leetcode BFS解题思路

    Word Ladder 思路一:单向bfs, 使用visited数组记录哪些已经访问过了, 访问过的就不允许再次入队, 同时这里想到的是使用26个英文字母,枚举可能的取值, 类似brute force ...

  4. [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 ...

  5. [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 ...

  6. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  7. [LeetCode] Word Break 解题思路

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  8. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [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 ...

随机推荐

  1. mysql SELECT INTO OUTFILE ,can't create file (转)

    原文  http://blog.sina.com.cn/s/blog_6a5e34ad0100zfbi.html (转) 命令行模式进入mysql #mysql -uroot -p12345 #sel ...

  2. 【实习记】2014-08-29算法学习Boyer-Moore和最长公共子串(LCS)

        昨天的问题方案一:寻找hash函数,可行性极低.方案二:载入内存,维护成一个守护进程的服务.难度比较大.方案三:使用前5位来索引,由前3位增至前5位唯一性,理论上是分拆记录扩大100倍,但可以 ...

  3. 【自用代码】Json转对象

    private static object JsonToObject(string jsonString, object obj) { var serializer = new DataContrac ...

  4. php MVC 及例子解释

    根据http://www.21ds.net/article/4/453改写: MVC模式在网站架构中十分常见.它允许我们建立一个三层结构的应用程式,从代码中分离出有用的层,帮助设计师和开发者协同工作以 ...

  5. UM分享 - 详解

    官网: http://dev.umeng.com 友盟现在发展的很壮大! 有熟为人知的社会化分享\统计分析\消息推送\即时通信\自动更新\多媒体服务等功能, 今天就其中第一项 分享功能, 做出分解. ...

  6. ubuntu13.10 登陆后黑屏,没有菜单栏,可以启动termina,怎么解决?

    最近在学习openGL,自己的电脑是intel集显加nvidia GT630M,本来想应该可以支持到opengl4以上的,可是发现nvidia的显卡由于驱动问题,好像一直没有用到,所以只支持了open ...

  7. 【C#】动态加载dll程序集

    原文:http://www.cnblogs.com/bomo/archive/2013/03/01/2938165.html 很多时候我们需要用到引用其他程序集,有些程序集是.Net生成的,可以支持反 ...

  8. [BZOJ 2738] 矩阵乘法 【分块】

    题目链接:BZOJ - 2738 题目分析 题目名称 “矩阵乘法” 与题目内容没有任何关系..就像VFK的 A+B Problem 一样.. 题目大意是给定一个矩阵,有许多询问,每次询问一个子矩阵中的 ...

  9. Codeforces Round #197 (Div. 2) : B

    也是水题一个,不过稍微要细心点.... 贴代码: #include<iostream> using namespace std; long long n,m; ; int main() { ...

  10. Python Web 性能和压力测试 multi-mechanize

    http://www.aikaiyuan.com/5318.html 对Web服务做Performance & Load测试,最常见的工具有Apache Benchmark俗称ab和商用工具L ...