LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/
二.题目大意:
给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数的和等于target,找出所有的四元组,当然这些四元组不能有重复的。
三.题解:
这道题实质就是3sum的变形,关于4sum问题,已经在https://www.cnblogs.com/wangkundentisy/p/9079622.html这里说过了,无外乎最外面两层循环,最里面的循环使用哈希表或者双指针,此处使用的是双指针法。具体代码如下:
class Solution
{
public:
vector<vector<int>> fourSum(vector<int>& nums, int target)
{
int len = nums.size();
vector<vector<int>> rs;
if(len < 4)
return rs;
sort(nums.begin(),nums.end());
for(int i = 0; i < len; i++)
{
if(i != 0 && nums[i] == nums[i - 1])
continue;
for(int j = i + 1; j < len - 1; j++)
{
if(j != i + 1 && nums[j] == nums[j -1])//注意此处的去重,要保证j不是第一次被使用,所以必须是j!=i+1
continue;
int l = j + 1, r = len - 1;
while(l < r)
{
if(nums[i] + nums[j] + nums[l] + nums[r] == target)
{
rs.push_back({nums[i],nums[j],nums[l],nums[r]});
l++;
r--;
while( l < r && nums[l] == nums[l - 1])
l++;
while(l < r && nums[r] == nums[r + 1])
r--;
}
else if(nums[i] + nums[j] + nums[l] + nums[r] < target)
l++;
else
r--; }
}
}
return rs; }
};
本例中的方法的时间复杂度为O(N^3),空间复杂度为O(1)。此外,之前在3sum问题中总结过:4sum问题还有时间复杂度为O(N^2)的算法,具体可见:https://www.cnblogs.com/wangkundentisy/p/9079622.html
这里有几点需要注意:
1.由于题目要求四元组是唯一的,所以要进行去重:每一个外层循环都需要去重,即i和j都要去重(对于ksum问题,也是这样的,k-1个外层循环都要按照这个规律进行去重)。
2.外层循环的去重,要保证此处的指针不是第一次被使用,也就是说在该位置之前,应该确保已经执行过一次了(如果是第一次使用,就不用去重了),所以才用 i != 0和 j != i + 1这种判断条件,对于ksum问题,一定注意这种规律。
LeetCode——18. 4Sum的更多相关文章
- [LeetCode] 18. 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 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- [LeetCode] 18. 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 18. 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 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- Leetcode 18. 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 ...
- Java [leetcode 18]4Sum
问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- C#解leetcode 18. 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]18. 4Sum四数之和
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
随机推荐
- php curl 传输是url中带有空格的处理方法
在crul中,应该用%20代替空格,否则,空格以后的数据将get不到.
- Apache Kafka 源码剖析
Getting Start 下载 http://kafka.apache.org/ 优点和应用场景 Kafka消息驱动,符合发布-订阅模式,优点和应用范围都共通 发布-订阅模式优点 解耦合 : 两个应 ...
- Node学习笔记:建立TCP服务器和客户端之间的通信
结构: socket是应用层和传输层的桥梁.(传输层之上的协议所涉及的数据都是在本机处理的,并没进入网络中) 涉及数据: socket所涉及的数据是报文,是明文. 作用: 建立长久链接,供网络上的两个 ...
- oracle实用命令入门
登录oracle(需要在oracle用户下) 执行sqlplus,然后输入用户名和密码就可以了,如果是第一次使用oracle的话,可以直接使用sqlplus / as sysdba免密码以管理员的身份 ...
- BeEF介绍
什么是BeFF? BeEF是眼下欧美最流行的web框架攻击平台,它的全称是 the Browser exploitation framework project.近期两年国外各种黑客的 ...
- Oracle sqlnet.ora配置
Oracle sqlnet.ora配置 sqlnet.ora的作用(官网指出的) www.2cto.com 1.限制客户端访问(如指定客户端域为不允许访问) 2.指定命名方法(local nami ...
- mysql之 openark-kit online ddl
MySQL工具集openark-kit (官方网站 http://code.openark.org/forge/openark-kit),内部包含很多小工具,在5.6之前用于实现online ddl操 ...
- centos6 下 yum 升级php5 到 php7
一 [卸载老版本php] 1.查看老版本 2.卸载老版本 yum remove php* 本人使用一下方式卸载老版本 1)yum -y remove php* #这个命令可以卸载部分,但是清除不干净 ...
- php调用C#生成的dll(二)
根据上一篇博文,php调用c#生成的动态库,运行测试时报Fatal error: Class 'COM' not found in XXXXXXXXX 原因是php.ini中未开启COM组件的应用,解 ...
- Spring4相关jar包介绍(转)
Spring4相关jar包介绍 spring-core.jar(必须):这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件要都要使用到这个包里的类,是其它组件的基本核心,当 ...