LeetCode OJ 之 4Sum (”四个数的和“)
来源:互联网 发布:es6 map遍历数组 编辑:程序博客网 时间:2024/05/17 07:21
题目:
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)
思路:
思路类似3sum:http://blog.csdn.net/u012243115/article/details/41361781 ,但是这里不能用那种方法去重.
代码:
class Solution {public:vector<vector<int>> fourSum(vector<int>& num, int target) {vector<vector<int> > result;if (num.size() < 4) return result;sort(num.begin(), num.end());vector<int>::iterator last = num.end();for (vector<int>::iterator a = num.begin(); a < last - 3 ; ++a) {for (vector<int>::iterator b = a + 1 ; b < last - 2 ; ++b) {vector<int>::iterator c = b + 1 ;vector<int>::iterator d = last - 1 ;while (c < d) {if (*a + *b + *c + *d < target){++c;} else if (*a + *b + *c + *d > target) {--d;} else {result.push_back({ *a, *b, *c, *d });++c;--d;}}}}sort(result.begin(), result.end());//注意sort不是vector的库函数,而是algorithm库的泛型算法sort//unique的功能是去除相邻的重复元素(只保留一个),其实它并不真正把重复的元素删除,而是把重复的元素移到后面去了//然后返回的迭代器指向超出无重复的元素范围末端的下一个位置。因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。//然后调用erase删除后面的重复的元素result.erase(unique(result.begin(), result.end()), result.end());return result;}};
代码2:
//map 做缓存// LeetCode, 4Sum// 用一个 hashmap 先缓存两个数的和// 时间复杂度,平均 O(n^2) ,最坏 O(n^4) ,空间复杂度 O(n^2)class Solution {public:vector<vector<int> > fourSum(vector<int> &num, int target) {vector<vector<int> > result;if (num.size() < 4) return result;sort(num.begin(), num.end());unordered_map<int, vector<pair<int, int> > > cache;//把num中任意两个数的和作为键,这两个数的下标对作为值保存在cache中,(小的在前,大的在后)如果有两对数的和相同,则键相同,下标对放在同一个vector里for (size_t a = 0; a < num.size(); ++a) {for (size_t b = a + 1; b < num.size(); ++b) {cache[num[a] + num[b]].push_back(pair<int, int>(a, b));}}for (int c = 0; c < num.size(); ++c) {for (size_t d = c + 1; d < num.size(); ++d) {int key = target - num[c] - num[d];//如果key能在cache中的键中找到,则找到符合条件的,否则继续查找if (cache.find(key) == cache.end()) continue;vector<pair<int, int> > vec = cache[key];//vec里面可能有多个pairfor (size_t k = 0; k < vec.size(); ++k) {if (c <= vec[k].second)continue; // 为了防止重叠,可以让 c > vec[k].second 或者 d < vec[k].firstresult.push_back( { num[vec[k].first],num[vec[k].second], num[c], num[d] });//if (d >= vec[k].first)//continue; // result.push_back( { num[c], num[d] , num[vec[k].first],num[vec[k].second] });}}}sort(result.begin(), result.end());result.erase(unique(result.begin(), result.end()), result.end());return result;}};
0 0
- LeetCode OJ 之 4Sum (”四个数的和“)
- LeetCode | 4Sum(四个数的和)
- LeetCode OJ 之 Two Sum(两个数的和)
- LeetCode OJ 之 3Sum (三个数的和)
- 【LeetCode-面试算法经典-Java实现】【018-4Sum(四个数的和)】
- LeetCode OJ 之 3Sum Closest (三个数的和--二)
- leetcode解题之 18. 4Sum Java版(结果是目标值的四个数字和)
- LeetCode OJ-18.4Sum(四数和)
- LeetCode OJ 之 Path Sum(求路径和)
- LeetCode OJ 之 Path Sum II (路径和 - 二)
- LeetCode OJ 之 Combination Sum (“连接”和)
- LeetCode OJ 之 Combination Sum II (“连接”和 - 二)
- LeetCode OJ 之 Minimum Path Sum (最小路径和)
- LeetCode OJ 之Sum Root to Leaf Numbers (根到叶上数字的和)
- LeetCode OJ-15.3Sum(三数和问题)
- LeetCode OJ-16.3Sum Closest(最接近三数和)
- LeetCode OJ:4Sum
- LeetCode OJ 4Sum
- flex 捕获摄像头 清晰度参数调整
- BeagleBone Black快速入门教程第1章嵌入式Linux之于Maker们
- 使用树莓派实现微信远程监控
- ngx的初步源码分析1
- Python之网络爬虫
- LeetCode OJ 之 4Sum (”四个数的和“)
- 计算字符串的字符数量
- Linux查看.so文件中函数
- linux 库文件的生成与使用
- LEETCODE: Add Two Number
- 边界对齐
- MFC 学习之 界面美化<2>
- android 内存分析(MAT工具的使用)
- Python 性能鸡汤