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 = target? Find all unique quadruplets in the array which gives the sum of target.
给定一个数组,找出其中的四个数,使它们的和等于某个特定的数
解法
和2Sum以及3Sum一样,都是先排序然后遍历前面几个数,剩下的两个数用Two Pointers来找,能降低一个N的复杂度,这里复杂度为O(N^3)
这里采用了一种比3Sum这题里更简洁的判重方法,直接将选出的数用Map记录,每次选取前检查一次Map看是否存在。虽然效率有所牺牲,但是代码变得很简洁而且有高可读性,自认为有些时候这种交换是值得的。
class Solution
{
public:
vector<vector<int>> fourSum(vector<int>& nums, int target)
{
vector<vector<int>> rt;
map<vector<int>,bool> map;
sort(nums.begin(),nums.end());
for(int i = 0;i < nums.size();i ++)
for(int j = i + 1;j < nums.size();j ++)
{
int k = j + 1;
int l = nums.size() - 1;
while(k < l)
{
if(nums[i] + nums[j] + nums[k] + nums[l] == target)
{
vector<int> temp = {nums[i],nums[j],nums[k],nums[l]};
if(map.find(temp) == map.end())
{
map[temp] = true;
rt.push_back(temp);
}
k ++;
}
else if(nums[i] + nums[j] + nums[k] + nums[l] < target)
k ++;
else
l --;
}
}
return rt;
}
};
LeetCode 4Sum (Two pointers)的更多相关文章
- LeetCode——4Sum & 总结
LeetCode--4Sum & 总结 前言 有人对 Leetcode 上 2Sum,3Sum,4Sum,K Sum问题作了总结: http://blog.csdn.net/nanjunxia ...
- [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 ...
- [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 ...
- leetcode — 4sum
import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * Source : https://oj.l ...
- Leetcode 8 Two Pointers
Two Pointers 1. 28. Implement strStr() 用 i 记录haystack偏移量,j 记录 needle 的偏移量. class Solution { public i ...
- LeetCode 4Sum 4个数之和
题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...
- [LeetCode] 4Sum hash表
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- 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 ...
- 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 ...
随机推荐
- Prometheus Node_exporter 之 Network Netstat TCP Linux MIPs
Network Netstat TCP Linux MIPs1. TCP Aborts / Tiemouts type: GraphUnit: shortLabel: ConnectionsTCPAb ...
- 购物商城学习--第二讲(maven工程介绍)
接下来第二讲介绍整体工程如何使用maven搭建的. 使用maven管理工程的好处: jar包的管理: 工程之间的依赖管理: 自动打包 maven常见打包方式:jar.war和pom三种.jar工程,是 ...
- Freemarket语法
<#--freemarker HashMap取值--> <#assign maps={"1":"张三丰","2":&quo ...
- Another reason why SQL_SLAVE_SKIP_COUNTER is bad in MySQL
It is everywhere in the world of MySQL that if your replication is broken because an event caused a ...
- Centos/Linux下调整分区大小(以home和根分区为例)
转载于 https://blog.csdn.net/qq_33233768/article/details/65437609 在安装新系统的时候,有时候没法预估或者说错误的划分了分区大小,常常会 ...
- env :让系统决定你命令的位置
如果我们使用lua 运行脚本程序,或者使用python执行程序.我们不用明确 python的位置,可以使用env 命令来运行: env 程序介绍: NAME env - run a program i ...
- tp查询顺序
tp竟然可以跟据商品的id来进行排序查询的数据 记一些php中排序: sort()方法一维索引数组进行值升序排序 rsort()方法一维索引数组进行值降序排序 unsort()方法负责编写自定义排序操 ...
- sphinx编译安装
install sphinx wget http://sphinxsearch.com/files/sphinx-2.0.8-release.tar.gz tar zxvf sphinx--relea ...
- myeclipse10配置maven
一:Maven的下载安装 准备工作: 1)安装环境 Windows xp 2)需安装JDK ,并配置环境变量(略) 3) Maven版本3.0.5 4)下载地址:http://mirror.bit.e ...
- python中for、while循环、if嵌套的使用
1.for循环字符串就是一个有序的字符序列for i in range(5): print(i)定义一个死循环while True: pass2.break和continue肯定需要和 ...