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 ...
随机推荐
- 多个div中的label标签对齐
这是之前的页面效果: 添加红色部门的代码后: <head> <meta name="viewport" content="width=device-wi ...
- Hsqldb中设置主键,并让主键自增
CREATE TABLE userinfo ( Id INTEGER GENERATED BY DEFAULT AS IDENTITY, Name varchar(100) NOT NULL, Dep ...
- Ansible自动化运维工具使用
概述本文描述自动化运维工具 Ansible 的安装及基础使用方法,包含: Centos 下的安装主机配置Ad-Hoc command(命令行执行)Playbook (任务剧本)Ansible 和 Sa ...
- WLW/OLW 最佳博客写作软件
前言 我发布到博客园中文章大多是通过Windows live Writer(wlw)来写的,本文记录一下wlw的安装及快捷键. WLW博客园插入代码插件:http://www.cnblogs.com/ ...
- Markdown 进阶
目录 markdown进阶语法 内容目录 加强代码块 脚注 流程图 时序图 LaTeX公式 markdown进阶语法 内容目录 使用 [TOC] 引用目录,将 [TOC] 放至文本的首行,编辑器将自动 ...
- 微信支付回调,XXE攻击漏洞防止方法
最近微信支付回调发现的XXE攻击漏洞(什么是XXE攻击,度娘.bing去搜,一搜一大把),收到通知后即检查代码, 微信给的解决方法是如果你使用的是: XmlDocument: XmlDocument ...
- js判断360浏览器 兼容模式IE版本
var ms_ie = false; var ua = window.navigator.userAgent.toLowerCase(); var old_ie = ua.indexOf('MSIE' ...
- JavaScript利用Date实现简单的倒计时实例
介绍 Date对象,是操作日期和时间的对象.Date对象对日期和时间的操作只能通过方法.Date在js中和Array类似,都是拥有自己的特殊方法的特殊对象. 创建 Date 对象的语法: var my ...
- 百度地图POI数据爬取,突破百度地图API爬取数目“400条“的限制11。
1.POI爬取方法说明 1.1AK申请 登录百度账号,在百度地图开发者平台的API控制台申请一个服务端的ak,主要用到的是Place API.检校方式可设置成IP白名单,IP直接设置成了0.0.0.0 ...
- python提示警告InsecureRequestWarning
在Python3中使用以下代码报错: import requests response = requests.get(url='', verify=False) 错误代码如下: InsecureReq ...