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 ...
随机推荐
- windows10如何打开vhd文件
本人电脑安装了Visual Studio 2017,但是由于项目需求需要Core SDK(2.0)的版本支持,也就是2017最新版.所以现在需要利用visual Studio 2017最新版本的安装包 ...
- Oracle EBS AR 收款取数
-- 收款核销,贷项通知单核销也是通过ar_receivable_applications_all表 SELECT cr.receipt_number ,ad.amount_dr ,ad.amount ...
- MySQL之UNDO及MVCC、崩溃恢复
UNDO特性:避免脏读.事务回滚.非阻塞读.MVCC.崩溃恢复 事务工作流程(图2) MVCC原理机制 崩溃恢复:redo前滚.undo回滚 长事务.大事务:危害.判断.处理 UNDO优化:实现u ...
- jquery 比较全面的API中文版地址
jquery中文API地址 里面有对各个版本的jquery的属性.方法等都有全面的介绍加例子,值得拥有!
- Android图形显示之硬件抽象层Gralloc【转】
https://blog.csdn.net/yangwen123/article/details/12192401 FrameBuffer驱动程序分析文中介绍了Linux系统下的显示驱动框架,每个显示 ...
- oracle like模糊查询简单用法
like 用法介绍: 1.“_”:匹配单个任意字符 select * from bqh3 where name like '_崔'; 2.“%”:匹配0个或多个任意字符.但有三种情况如下: like ...
- Mysql学习---面试基础知识点总结
drop.truncate. delete区别 TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 ...
- luogu P3690 【模板】Link Cut Tree (动态树)
嘟嘟嘟 LCT竟然看了整整一天,但好歹是看懂了. 教程这里不写,强烈推荐 闪狐大佬的博客 . 但是还是有几句想说的. 1.尽管LCT和splay很像,但是有一些细节还是不一样的.首先是rotate,我 ...
- JVM系列三:JVM参数设置
JVM系列三:JVM参数设置.分析 不管是YGC还是Full GC,GC过程中都会对导致程序运行中中断,正确的选择不同的GC策略,调整JVM.GC的参数,可以极大的减少由于GC工作,而导致的程序运 ...
- ViewData、ViewBag、TempData、Session的区别与联系
简介 这篇文章是我在学习ASP.NET MVC程序传值方式梳理总结的笔记.在ASP.NET MVC中,页面间和Controller与View之间主要有以下几种小量数据传值方式, ViewData.Vi ...