算法(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 ...
随机推荐
- 2017年PHP程序员未来路在何方?(转载)
PHP 从诞生到现在已经有 20 多年历史,从 Web 时代兴起到移动互联网退潮,互联网领域各种编程语言和技术层出不穷, Node.js . GO . Python 不断地在挑战 PHP 的地位.这些 ...
- spark-day1
#!/usr/bin/python # -*- coding: utf_8 -*- from pyspark import SparkConf, SparkContext import os, tim ...
- flask 中访问时后台错误 error: [Errno 32] Broken pipe
解决办法:app.run(threaded=True) 个人理解:flask默认单线程,访问一个页面时会访问到很多页面,比如一些图片,加入参数使其为多线程
- linux下磁盘分区、格式化、挂载
(1)fdisk /dev/sdb进行分区 (2)选择n表示添加一个分区,选择d表示删除一个分区.可通过m获取帮助信息 (3)选择p表示主分区,然后输入分区大小 (4)分区完成后,可通过fdisk - ...
- linux-课题练习1
1.创建组testgroup: 2.创建用户a2012,先采用默认设置创建,然后使该用户加入testgroup组. 3.创建用户a2013,其用户主目录为/tmp/a2013,其主组为testgrou ...
- Kubernetes-DNS
Kubernetes提供的虚拟DNS服务名为skydns,由四个组件组成: etcd:DNS存储 kube2sky:将Kubernetes Master中的Service(服务)注册到etcd sky ...
- Python3.6中PyInstaller不能对文件进行打包问题
上篇文章<itchat和matplotlib的结合使用爬取微信信息>是用python爬取信息得到微信朋友的信息,并且用matplotlib统计信息进行画图,所以今天想将它打包成.exe可执 ...
- 集合--数据结构与算法JavaScript描述(9)
集合 Set 概念 (跟高中时数学课上学的集合几乎一模一样哦哈哈) 集合是一种包含不同元素的数据结构. 集合中的元素称为成员. 集合的两个最重要的特性: 集合中的成员是无序的. 集合中不允许相同成员存 ...
- ISE中FPGA的实现流程
一.ISE实现的步骤 在综合之后,我们开始启动FPGA在ISE中的实现过程,整个过程包括以下几个步骤: 1.Translate ...
- ORA-12705: Cannot access NLS data files or invalid
RedHat7.1 Oracle11gr2 oracle 默认的编码方式如下:SQL> select userenv('language') from dual; USERENV('LANGUA ...