博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode]Map-560. Subarray Sum Equals K
阅读量:4585 次
发布时间:2019-06-09

本文共 931 字,大约阅读时间需要 3 分钟。

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

Example 1:

Input:nums = [1,1,1], k = 2Output: 2

 

Note:

  1. The length of the array is in range [1, 20,000].
  2. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].

 

class Solution {public:    int subarraySum(vector
& nums, int k) { int n=nums.size(); if (n==0) return 0; vector
sum(n+1,0); for (int i=1; i<=n; i++) sum[i]=nums[i-1]+sum[i-1]; unordered_map
hash; hash[0]=1; int res=0; for (int i=1; i<=n; i++) { if (hash[sum[i]-k]) res+=hash[sum[i]-k]; hash[sum[i]]++; } return res; }};

 

转载于:https://www.cnblogs.com/chenhan05/p/8280321.html

你可能感兴趣的文章
从PRISM开始学WPF(四)Prism-Module?
查看>>
解决session阻塞的问题
查看>>
SQL Server 触发器
查看>>
css优先级计算规则
查看>>
Asp.Net Web API 2第十五课——Model Validation(模型验证)
查看>>
Silverlight 4 MVVM开发方式(三)动态换皮
查看>>
ExtJs中OA管理中组织和用户关系左右选择组件的运用
查看>>
【原创】关于高度自适应问题
查看>>
Tomcat JMX
查看>>
2019 年,容器技术生态会发生些什么?
查看>>
ubuntu安装hive
查看>>
Java NIO Selector(八)
查看>>
Hibernate入门(三)—— 一对多、多对多关系
查看>>
Openstack中查看虚拟机console log的几种方法
查看>>
科技创新平台年报系统利益相关者分析
查看>>
家庭作业第三章3.57
查看>>
ERROR! MySQL manager or server PID file could not be found!
查看>>
nginx server_name匹配顺序
查看>>
数据备份希望使用gistore备份mongo数据
查看>>
【杂谈】新学年的第一篇博客
查看>>