Leetcode:4Sum与3Sum
来源:互联网 发布:网络表情包原型 编辑:程序博客网 时间:2024/06/16 15:45
4Sum:
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
- Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
- The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2)先对数组排序,之后进行两重for循环,然后对最后的一个数组设两个指针遍历,初始一个指针指向第二重循环后面一个位置,另一个指针则指向数组最后一个元素,因为数组已经排序,根据这四个数的和与target大小比较,来移动这两个指针:如果比目标数值小,则c++,如果比目标数值大则d--。这里注意重复的问题,在一重循环中如果和前面一个数相同则跳过,因为前面的查找肯定包含了本次的情况。
class Solution {public: vector<vector<int> > fourSum(vector<int> &num, int target) { vector<vector<int> > ret; if (num.size() == 0) return ret; sort(num.begin(), num.end()); for (size_t a = 0; a < num.size(); ++a) { if (a != 0 && num[a] == num[a - 1]) { continue; } for (size_t b = a + 1; b < num.size(); ++b) { if (b != a + 1 && num[b] == num[b - 1]) { continue; } size_t c = b + 1; size_t d = num.size() - 1; while (c < d) { const int sum = num[a] + num[b] + num[c] + num[d]; if (sum > target) { --d; } else if (sum < target) { ++c; } else if (c != b + 1 && num[c] == num[c - 1]) { ++c; } else if (d != num.size() - 1 && num[d] == num[d + 1]) { --d; } else { vector<int> result; result.push_back(num[a]); result.push_back(num[b]); result.push_back(num[c]); result.push_back(num[d]); ret.push_back(result); ++c; --d; } } } } return ret;}};
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)方法基本一样,实现代码如下:
class Solution {public: vector<vector<int> > threeSum(vector<int> &num) { vector<vector<int> > result; if(num.size() <= 2){ return result; } sort(num.begin(), num.end()); for(int i = 0; i < num.size()-2; ++i){ if(i > 0 && num[i] == num[i-1]){ continue; } int begin = i+1; int end = num.size()-1; while(begin < end){ int sum = num[i] + num[begin] + num[end]; if( sum == 0){ result.push_back({num[i], num[begin], num[end]}); begin++; while(begin < end && num[begin] == num[begin-1]){ begin++; } end--; while(end > begin && num[end] == num[end+1]){ end--; } }else if(sum < 0){ begin++; }else{ end--; } } } return result; }};
0 0
- Leetcode:4Sum与3Sum
- leetcode 2 sum 3sum 4sum
- Leetcode 2SUM-3SUM-4SUM
- Leetcode-2sum,3sum,4sum
- leetcode 2 sum & 3 sum & 4 sum
- [LeetCode] 2Sum, 3Sum, 4Sum, 3SUm closet
- [LeetCode] K sum(2Sum、3Sum、4Sum)
- leetcode--sum集合:2sum,3sum,4sum
- leetcode --- 2 sum , 3 sum , 4 sum , k sum problem
- 【Leetcode】4Sum (Sum)
- 【Leetcode】3Sum (Sum)
- LeetCode(3 Sum, 4sum, ksum)
- leetcode 3sum closest && 4sum
- leetcode 15 3 sum 4 sum
- [LeetCode] Two Sum, 3Sum ,3SumCloset , 4Sum
- 【LeetCode】3Sum && 3Sum Closest && 4Sum
- leetcode 3Sum 3Sum Closest 4Sum
- LeetCode OJ - 3Sum、3Sum Closest、4Sum
- Linux常用命令(三十二) - wc
- [iOS开发]IOS多线程之Block编程
- ceph存储 ceph集群pool、pg、crush设置
- iOS学习爬坑记录6: 为Xcode 6.1添加Empty Application project template
- android的EditText控件技巧
- Leetcode:4Sum与3Sum
- 出现未将对象对象引用设置到对象的实例
- hive中与hbase外部表join时内存溢出(hive处理mapjoin的优化器机制)
- Big Number 1018
- 问卷-设计开发,数据库设计到前台页面
- syslog
- 我喜欢的乐队-Descending
- 网页设计小工具
- 堆和栈的区别(转过无数次的文章)