算法(10)Subarray Sum Equals K
题目:在数组中找到一个子数组,让子数组的和是k。
思路:先发发牢骚,这两天做题是卡到不行哇,前一个题折腾了三天,这个题上午又被卡住,一气之下,中午睡觉,下午去了趟公司,竟然把namespace和cgroup的架构给搞懂了!所以晚上再来攻克这个问题!上午的做法是这样的,设置一个fast指针,一个slow指针,当【slow,fast】中的值大于k的时候,fast++,反之slow++,这种错误的解法误以为数组是有序的,所以是行不通的,那么这道题的解法是什么呢?然后写了下O(n^3)的解法,果不其然,计算超时!!!没办法,只能查资料啦!
真正被接收的答案真是让人始料未及:
int sum = 0;
int n = nums.size();
map<int,int> m;
int res = 0;
for (int i = 0; i < n; i++) {
m[sum]++;
sum += nums[i];
res += m[sum-k];
}
return res;
map是做啥的啊,以【1,1,1】为例吧,第一轮
m[0]=1, sum=1 res=m[1-2=-1]=0;
m[1]=1, sum=1+1=2 res=m[2-2=0]=1;
m[2]=1, sum=2+1=3 res+=m[3-2=1]=1+1=2;
算法(10)Subarray Sum Equals K的更多相关文章
- leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- Subarray Sum & Maximum Size Subarray Sum Equals K
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
- Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
- [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- Subarray Sum Equals K LT560
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- [leetcode]560. Subarray Sum Equals K 和为K的子数组
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- LeetCode 560. Subarray Sum Equals K (子数组之和等于K)
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- [LeetCode] Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- 560. Subarray Sum Equals K 求和为k的子数组个数
[抄题]: Given an array of integers and an integer k, you need to find the total number of continuous s ...
随机推荐
- Easyui多个下拉框联动效果
好久没写前端了,以前在做多级联动的时候,用的是easyui的tree结构,但是需要一次性全部加载,不是按需加载,性能不好,退而求其之,用多个下拉框做 eayui的combobox 有onSelect ...
- 子域收集-fierce
fierce 是使用多种技术来扫描目标主机IP地址和主机名的一个DNS服务器枚举工具.运用递归的方式来工作.它的工作原理是先通过查询本地DNS服务器来查找目标DNS服务器,然后使用目标DNS服务器来查 ...
- 6 大主流 Web 框架优缺点对比(转)
英文: Kit Kelly 译文:oschina https://www.oschina.net/translate/web-frameworks-conclusions 是该读些评论和做一些总结 ...
- Laravel5.x 封装的上传图片类
图片缩放需要用conposer安装 ImageManagerStatic类 可参考下面的地址安装: https://www.jb51.net/article/128159.htm 控制器里: 控制器里 ...
- Idea 2017 激活方法
http://www.cnblogs.com/suiyueqiannian/p/6754091.html
- ruby 第三方模块unirest使用
Creating Requests require 'unirest' response = Unirest.post 'http://httpbin.org/post', headers:{ Acc ...
- Apache Struts最新漏洞 远程代码执行漏洞预警 2018年11月08日
2018年11月8日,SINE安全监控检测中心,检测到Apache Struts官方更新了一个Struts漏洞补丁,这个漏洞是Apache Struts目前最新的漏洞,影响范围较广,低于Apache ...
- 加载旋转框(loading spinner)
目标是这样的 用到的组件 AlertDialog 和 ProgressBar 先创建一个 AlertDialog 的布局 <?xml version="1.0" encodi ...
- PHP.37-TP框架商城应用实例-后台13-商品管理-扩展分类的添加、显示【数据分组】、搜索分类【多对多】
商品扩展分类 需求:一件商品能有多个扩展分类,搜索任何一个分类都能搜出该商品 建表[扩展分类表] drop table if exists p39_goods_cat; create table p3 ...
- 2457: [BeiJing2011]双端队列
2457: [BeiJing2011]双端队列 链接 很奇妙的转化. 题目要求最后的所有序列也是有序的,所以可以求出最后的序列(即排序后的序列),然后分成许多份,要求每一份都是一个双端序列,求最少分成 ...