leetcode c++ 3sum
2014-11-05 23:37
411 查看
this problem is so easy that we just need i,j,k these 3 pointers.
when i is fixed that if we want get the precise value, we just need to move the pointer j and the pointer k.
1.But as we all know, that we don't need the duplicate answer.
when the array is sorted, we can judge whether num[j]equals num[j-1] and the same to k.
2.when we find a solution on a stable i,we should't stop looking for, So use break will lost some proper solution.so we just let j++ork--class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
sort(num.begin(),num.end());
vector<vector<int>> answer;
if(num.size()<3)
return answer;
//there is a bad situation. 0 0 0 0;when a[i]is the same as a[i+1] we can just continue
for(int i = 0;i<num.size()-2;i++)
{
if(num[i] == num[i-1]&&i>0)
continue;
int j = i+1;
int k = num.size()-1;
while(j<k)
{
//we must have the different j and different k and differen i
if(num[j] == num[j-1] && j-1>i)
{j++;continue;}
if(num[k] == num[k+1] && k+1<num.size()-1)
{k--;continue;}
if(num[j]+num[k] < -num[i])
{ j++; continue;}
if(num[j]+num[k] > -num[i])
{k--; continue;}
if(num[j]+num[k] == -num[i])
{
vector<int> temp;
temp.push_back(num[i]);
temp.push_back(num[j]);
temp.push_back(num[k]);
answer.push_back(temp);
// break; we can't break here,because there might be more than one vector when i is fixed
j++;
}
}
}
return answer;
}
};
when i is fixed that if we want get the precise value, we just need to move the pointer j and the pointer k.
1.But as we all know, that we don't need the duplicate answer.
when the array is sorted, we can judge whether num[j]equals num[j-1] and the same to k.
2.when we find a solution on a stable i,we should't stop looking for, So use break will lost some proper solution.so we just let j++ork--class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
sort(num.begin(),num.end());
vector<vector<int>> answer;
if(num.size()<3)
return answer;
//there is a bad situation. 0 0 0 0;when a[i]is the same as a[i+1] we can just continue
for(int i = 0;i<num.size()-2;i++)
{
if(num[i] == num[i-1]&&i>0)
continue;
int j = i+1;
int k = num.size()-1;
while(j<k)
{
//we must have the different j and different k and differen i
if(num[j] == num[j-1] && j-1>i)
{j++;continue;}
if(num[k] == num[k+1] && k+1<num.size()-1)
{k--;continue;}
if(num[j]+num[k] < -num[i])
{ j++; continue;}
if(num[j]+num[k] > -num[i])
{k--; continue;}
if(num[j]+num[k] == -num[i])
{
vector<int> temp;
temp.push_back(num[i]);
temp.push_back(num[j]);
temp.push_back(num[k]);
answer.push_back(temp);
// break; we can't break here,because there might be more than one vector when i is fixed
j++;
}
}
}
return answer;
}
};
相关文章推荐
- [C++]LeetCode: 70 3Sum
- leetcode刷题系列C++-3Sum Closest
- LeetCode 3Sum 三个数和为零的集合 C++完整程序
- leetcode刷题系列C++-3sum
- LeetCode刷题(C++)——3Sum(Medium)
- 【LeetCode014-015算法/编程练习C++】最长共同前缀,3Sum(和为0) //用到了map的自动排序
- [LeetCode] 015. 3Sum (Medium) (C++/Java/Python)
- 【Leetcode 15】3Sum 三数和问题 C++
- 【C++】【LeetCode】15. 3Sum & 16. 3Sum Closest & 18. 4Sum
- 【LeetCode 15】3Sum (C++)
- LeetCode 15 — 3Sum(C++ Java Python)
- [LeetCode] 3Sum分析与C/C++解法
- leetcode_c++:3Sum(015)
- LeetCode刷题(C++)——3Sum Closest(Medium)
- leetcode - 3Sum Closest
- leetcode 57: 3Sum Closest
- LeetCode : 3Sum Closest
- Leetcode 15 3Sum
- leetcode 120: 3Sum
- LeetCode 3Sum