【LeetCode 18】四数之和
【题解】
两重循环枚举[i..j]这个区间
同时规定必取nums[i]和nums[j]
那么现在的问题就变成在下标为[i..j]这个区间的数字里面找两个数字使他们的和为target-nums[i]-nums[j].
这个问题可以在O(N)的复杂度解决。
所以复杂度就是$O(N^3)$
当然也可以用meet-in-middle写成O(N^2)的
在map里面存个和为x的两个数分别有什么(存他们俩的下标对)。
然后再次两重循环去找map中和target-nums[i]-nums[j]的pair有多少。
然后合并。
去掉有相同下标的情况(一个数字用了两次);
【代码】
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int> > ans;
ans.clear();
vector<int> temp;
temp.resize(4);
sort(nums.begin(),nums.end());
int len = nums.size();
for (int i = 0;i < len;i++){
if (i>0 && nums[i]==nums[i-1]) continue;
for (int j = i+3;j<len;j++){
while(j+1<len && nums[j+1]==nums[j]) j++;
//在i..j这个范围内找和为target-nums[i]-nums[j]的数字
int key = target-nums[i]-nums[j];
int l = i+1,r = j-1;
while (l<r){
if (nums[l]+nums[r]>key){
r--;
}else if (nums[l]+nums[r]<key){
l++;
}else {
temp[0] = nums[i];temp[1] = nums[l];
temp[2] = nums[r];temp[3] = nums[j];
ans.push_back(temp);
while (l+1<r && nums[l+1]==nums[l]) l++;
while (r-1>l && nums[r-1]==nums[r]) r--;
l++;r--;
}
}
}
}
return ans;
}
};
【LeetCode 18】四数之和的更多相关文章
- Java实现 LeetCode 18 四数之和
18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...
- LeetCode 18. 四数之和(4Sum)
题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...
- [Leetcode 18]四数之和 4 Sum
[题目] Given an array nums of n integers and an integer target, are there elements a, b, c, and d in n ...
- [LeetCode] 18. 四数之和
题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...
- LeetCode:四数之和【18】
LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...
- 【LeetCode】18.四数之和
题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- 【LeetCode】四数之和
[问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...
- [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】四数之和【排序,固定k1,k2,二分寻找k3和k4】
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...
随机推荐
- python 学习 -- 第一天 初涉
久闻python大名却一直没去了解,趁学校培训这个机会 开始正式学习python 第一天初步介绍一点关于python的东西 安装了环境及编译器 环境是在之前就安装了 只安装编译器anaconda 之后 ...
- PHP curl_multi_strerror函数
curl_multi_setopt — 返回描述错误码的字符串文本. 说明 string curl_multi_strerror ( int $errornum ) 返回描述 CURLM 错误码的字符 ...
- [CSP-S模拟测试53]题解
A.u 只涉及到区间修改可以考虑差分,然而如果每一行都差分复杂度还是过高.我们发现差分标记也是连续的(一行横着的一行斜着的),所以可以维护两个 差分的差分,扫两遍统计即可. #include<c ...
- \pset 、\x命令
\pset命令用于设置输出的格式 \pset border 0:表示输出内容无边框.示例如下: \pset border 1:表示边框只在内部.示例如下: \pset border 2:表示内外都有边 ...
- java BufferSegment
package org.rx.util; import java.util.function.Consumer; import static org.rx.core.Contract.require; ...
- HTML-参考手册: HTML 全局属性
ylbtech-HTML-参考手册: HTML 全局属性 1.返回顶部 1. HTML 全局属性 New : HTML5 新属性. 属性 描述 accesskey 设置访问元素的键盘快捷键. clas ...
- thymeleaf时间戳转换
<span th:text="${#dates.format(curDate, 'yyyy-MM-dd HH:mm:ss')}"></span> <t ...
- linux下安装JMeter(小白教程)
用windows平台测试时,会受到网络条件的影响,导致测试结果不够准确,尤其是高并发的情况下,需要能够精准的测试请求的响应时长,对于网络的要求更加苛刻.在这样的情况下,可以考虑在linux服务器端安装 ...
- 力扣算法——133.CloneGraph【M】
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph ...
- Cocos2d-x之引擎框架简介
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.cocos2d-x的设计思想 cocos2d-x分为导演,场景,图层,精灵,节点: (1).导演(Director):控制整个游戏的场 ...