LeetCode——4Sum & 总结

前言

有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结:

http://blog.csdn.net/nanjunxiao/article/details/12524405

对于同类问题做了代码模型:

int i = starting; //头指针
int j = num.size() - 1; //尾指针
while(i < j) {
int sum = num[i] + num[j];
if(sum == target) {
store num[i] and num[j] somewhere;
if(we need only one such pair of numbers)
break;
otherwise
do ++i, --j;
}
else if(sum < target)
++i;
else
--j;
}

题目

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)

• Thesolutionsetmustnotcontainduplicatequadruplets.

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)

思路

关于这道有很多解法:

解法一:K Sum

// 先排序,然后左右夹逼,时间复杂度 O(n^3),空间复杂度 O(1) 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());
auto last = num.end();
for (auto a = num.begin(); a < prev(last, 3); ++a) {
for (auto b = next(a); b < prev(last, 2); ++b) {
auto c = next(b);
auto d = prev(last);
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());
result.erase(unique(result.begin(), result.end()), result.end());
return result;
} };

补充: 关于unique()去重的使用,

參考 http://blog.csdn.net/zlhy_/article/details/8784553

解法二:Hashmap

用一个 hashmap 先缓存两个数的和, 以及vector<int, int>存这两个数。

在用两个游标遍历序列。key = target -v[x]-v[y], 依据 map.find(key), 找出另外两个数。

时间复杂度,平均 O(n^2),最坏 O(n^4),空间复杂度 O(n^2)

class Solution {
public:
vector<vector<int> > fourSum(vector<int> &sums, int target) {
vector<vector<int> > result;
if (nums.size() < 4) return result;
sort(nums.begin(), num.end()); unordered_map<int, vector<pair<int, int> > > cache;
for (int i=0; i<nums.size(); ++i) {
for (int j=i+1; j<nums.size(); ++j){
cache[nums[i]+num[j]].push_back(pair<int, int>(i, j));
}
} for (int x=0; x<nums.size(); ++x) {
for (int y=x+1; y<nums.size(); ++y) {
int key = target - nums[x] - nums[y];
if (cache.find(key) == cache.end()) continue; vector<pair<int, int> > vec = cache[key];
for (int k=0; k<vec.size(); ++k) {
if (x <= vec[k].second)
continue; //有重叠
result.push_back({ nums[vec[k].first], nums[vec[k].second], nums[c], nums[d]});
}
}
}
sort(result.begin(), result.end());
result.erase(unique(result.begin(), result.end()), result.end());
return result;
}
};

解法三:Multimap

首先要说的是 multimap的概念。

multimap提供了能够一种能够有反复键值的STL map类型。其插入方式和map类似,可是因为能够拥有反复键值所以在查找方面有些不同

  1. 直接找到每种键值的全部元素的第一个元素的游标

通过函数:lower_bound( const keytype& x ), upper_bound( const keytype& x ) 能够找到比指定键值x的小的键值的第一个元素和比指定键值x大的键值的第一个元素。返回值为该元素的游标。

细节:当到达键值x已经是最大时。upper_bound返回的是这个multimap的end游标。同理,当键值x已经是最小了,lower_bound返回的是这个multimap的begin游标。

  1. 指定某个键值,进行遍历

能够使用上面的lower_bound和upper_bound函数进行游历。也能够使用函数equal_range。其返回的是一个游标对。游标对pair::first是由函数lower_bound得到的x的前一个值,游标对pair::second的值是由函数upper_bound得到的x的后一个值。


这个算法的 时间复杂度 O(n^2),空间复杂度 O(n^2)

#include <iostream>
#include <vector>
#include <unordered_multimap> using namespace std; 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_multimap<int, pair<int, int>> cache;
for (int i=0; i+1<num.size(); ++i) {
for (int j=i+1; j<num.size(); ++j){
cache.insert(make_pair(num[i]+num[j], make_pair(i, j)));
}
} for (pair i =cache.begin(); i!=cache.end(); ++i){
int x = target - a->first;
pair range = cache.equal_range(x);
for (pair j = range.first; j!=range.second; ++j) {
int a = i->second.first;
int b = i->second.second;
int c = j->second.first;
int d = j->second.second;
if (a != c && a != d && b != c && b != d) {
vector<int> vec = { num[a], num[b], num[c], num[d] };
sort(vec.begin(), vec.end());
result.push_back(vec);
}
}
}
sort(result.begin(), result.end());
result.erase(unique(result.begin(), result.end()), result.end());
return result;
}
};

LeetCode——4Sum &amp; 总结的更多相关文章

  1. [LeetCode] 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  2. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  3. leetcode — 4sum

    import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...

  4. LeetCode 4Sum 4个数之和

    题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...

  5. Leetcode 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  6. Leetcode: 4Sum II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  7. leetcode 4sum python

    class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :t ...

  8. LeetCode 4Sum (Two pointers)

    题意 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  9. LeetCode——4Sum

    1. Question Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + ...

随机推荐

  1. git clone时RPC failed; curl 18 transfer closed with outstanding read data remaining

    git clone时报RPC failed; curl 18 transfer closed with outstanding read data remaining 错误 原因1:缓存区溢出 解决方 ...

  2. 导致线程死锁容易忽略的一点 SendMessage

    假如主线程 某一个按钮 点击的 响应要操作与另一个线程共享的 变量. 在这个点击响应里先lock 之后,假如另一个线程的变量正在 “使用”状态,并且内部又调用了SendMessage试图更新界面的某些 ...

  3. 遇到影响服务器性能的cpuspeed 服务

    最近碰到一个很蛋痛的问题,,我在公司的代码上实现了一个功能,然后基于这个测试,结果比对数据发现每天少三千多万条,, 然后我各种优化,各种零碎部功能阉割,,还是丢数据! 之后,监控运行网卡----wat ...

  4. 转:SpringMVC中日期格式的转换

    解决日期提交转换异常的问题 由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型.所以需要自定义参数绑定.前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestM ...

  5. [转]expect实现ssh自动交互

    shell脚本实现ssh自动登录远程服务器示例: #!/usr/bin/expect spawn ssh root@192.168.22.194 expect "*password:&quo ...

  6. ruby的sort方法的重新认识

    ruby中的sort方法,这个方法可以加一个两个参数的block,这个block可以返回1 0 -1来表示这两个参数大于 等于 小于示例: str = ["192.160.175" ...

  7. 利用图片中的exif元数据批量查找图片中所包含的GPS信息

    在图片的exif(交换图像文件格式)中标准定义了如何存储图像和音频文件的标准,而在这些标签中往往存在了一些容易被人们忽视却又重要的东西. 有一款工具名为exiftool,可以快速的解析所有标签,并将结 ...

  8. 命令行修改MySQL数据库密码

    通过MySQL命令行来修改MySQL数据库的密码,下面就为您详细介绍如何使用MySQL命令行来修改密码. 格式:mysqladmin -u用户名 -p旧密码 password 新密码 1.给root加 ...

  9. kubelet源码分析(version: git tag 1.7.6)

    一.概述 kubelet源码入口:cmd/kubelet/kubelet.go main() cmd/kubelet/app 包中的Run函数: 查看先参数,kubelet.KubeletDeps t ...

  10. 百度OCR识别示例

    文章地址:https://www.cnblogs.com/Charltsing/p/OcrAnswerer.html 最新版为v4.1版,开放一定概率的八窗口体验功能,请截图体验(多点几次图片).更新 ...