leetcode %15 in cpp
2016-05-21 05:52
447 查看
The solution first sorts the vector and then uses 2 pointer. For each number a, and its two pointer left and right( left starts at right next to a, and right starts at nums.size() - 1), if a + left + right > sum, it means that the current sum should be
reduced, or right should be moved one step left; if a + left + right < sum, it means that the current sum should be increased, or left should be moved one step right.
One of the trouble is how to avoid duplicates in the outputs. The solution is, whenever we move a, left or right, we should check if the next number is the same as the number we have checked previously. For a, each time we move from nums[i] to nums[i+1],
we check if nums[i] == nums[i+1]. If so then we move to nums[i+2] and check again. The same thing works for left and right.
code: class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
if(nums.size() < 3) return res;
sort(nums.begin(), nums.end());//sort the vector
int cur_sum;
int temp;
for(int i = 0; i < nums.size()-2; i ++){//if i == nums.size() -2, we have 2 numbers left to search and it is not possible to form a three sum. thus we terminate.
if(i-1 >= 0 && nums[i-1] == nums[i]) continue;//skipping the same number
cur_sum = -nums[i];
int left = i + 1; int right = nums.size() -1;
while(left < right){
temp = nums[left] + nums[right];
if(temp < cur_sum) left++;
else if(temp > cur_sum) right --;
else {
res.push_back(vector<int>{nums[i],nums[left],nums[right]});
while(left+1<nums.size() && nums[left] == nums[left+1]) left++;//skipping the same number to avoid duplicates in results
while(right-1>=0 && nums[right] == nums[right -1]) right--;//skipping the same number to avoid duplicates in results
left++;
right--;
}
}
}
return res;
}
};
reduced, or right should be moved one step left; if a + left + right < sum, it means that the current sum should be increased, or left should be moved one step right.
One of the trouble is how to avoid duplicates in the outputs. The solution is, whenever we move a, left or right, we should check if the next number is the same as the number we have checked previously. For a, each time we move from nums[i] to nums[i+1],
we check if nums[i] == nums[i+1]. If so then we move to nums[i+2] and check again. The same thing works for left and right.
code: class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
if(nums.size() < 3) return res;
sort(nums.begin(), nums.end());//sort the vector
int cur_sum;
int temp;
for(int i = 0; i < nums.size()-2; i ++){//if i == nums.size() -2, we have 2 numbers left to search and it is not possible to form a three sum. thus we terminate.
if(i-1 >= 0 && nums[i-1] == nums[i]) continue;//skipping the same number
cur_sum = -nums[i];
int left = i + 1; int right = nums.size() -1;
while(left < right){
temp = nums[left] + nums[right];
if(temp < cur_sum) left++;
else if(temp > cur_sum) right --;
else {
res.push_back(vector<int>{nums[i],nums[left],nums[right]});
while(left+1<nums.size() && nums[left] == nums[left+1]) left++;//skipping the same number to avoid duplicates in results
while(right-1>=0 && nums[right] == nums[right -1]) right--;//skipping the same number to avoid duplicates in results
left++;
right--;
}
}
}
return res;
}
};
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- C++中const用法总结
- CPP 虚函数、虚函数表及虚拟继承(转)
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- Deploying Control Plane Policing
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解