LeetCode 560. 和为K的子数组(Subarray Sum Equals K)
题目描述
给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数。
示例 1 :
输入:nums = [1,1,1], k = 2
输出: 2 , [1,1] 与 [1,1] 为两种不同的情况。
说明 :
- 数组的长度为 [1, 20,000]。
- 数组中元素的范围是 [-1000, 1000] ,且整数 k 的范围是 [-1e7, 1e7]。
解题思路
维护一个map,在遍历数组时,更新包含当前数字之前所有数的和出现的次数,这样每遍历到一个位置,将当前和减去k,若map中出现了此和,则说明一定会有一个连续的子数组和为k,所以使结果加上此和出现的次数。
代码
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int res = , sum = ;
map<int, int> kmap;
kmap[] = ;
for(int num: nums){
sum += num;
res += kmap[sum - k];
kmap[sum]++;
}
return res;
}
};
LeetCode 560. 和为K的子数组(Subarray Sum Equals K)的更多相关文章
- [Swift]LeetCode560. 和为K的子数组 | Subarray Sum Equals 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 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- [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 & 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] 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 ...
- [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 ...
- 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 ...
随机推荐
- Shell 编程中的常用工具
文件查找 find 命令 语法格式 find命令总结: 常用选项: -name 查找/etc目录下以conf结尾的文件 find /etc -name "*.conf" -inam ...
- pycharm连接数据库
今天说说如何使用pycharm连接oracle数据库 1.首先你需要安装oracle数据库 2.在pycharm中安装cx_Oracle.在file->settings->Project- ...
- 孤陋寡闻了吧?Python 居然可以做这30件神奇好玩的事情(附教程)
知乎上有个浏览超过400万的问题:可以用 Python 编程语言做哪些神奇好玩的事情? 我先举一个很不专业的栗子...... 然后再找几个人抬一堆例子来...... 不是很稀饭<复联>嘛, ...
- kvm虚拟机控制台登录配置
vm虚拟机能否像xen虚拟机一样通过virsh console 一样采用字符界面进行linux虚拟机控制台呢,答案是肯定的,默认情况下该命令是不起作用的,需要修改相关文件才能实现. 本文出自:http ...
- JVM系列一:虚拟机内存区域
虚拟机栈 1.虚拟机栈维护一个线程中所有方法的栈帧,每个栈帧中保存着这个方法中用到的局部变量表,操作数栈,常量引用 2.可以用-Xss来设置每个线程中虚拟机栈的大小,在jdk1.4之前默认虚拟机栈大小 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线) 链接:https://ac.nowcoder.com/acm/contest/163/F来源:牛客网 时间 ...
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)解决方案
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql. ...
- python_面向对象——属性方法property
1.属性方法 class Student(object): def __init__(self,name): self.name = name @property #属性方法:把一个方法变成一个静态的 ...
- http---返回网页(普通,多进程,多线程,协程方式实现)
代码: import socket import re import multiprocessing import threading import gevent from gevent import ...
- appium问题汇总(持续更新。。。)
WEBVIEW_unknown adb版本较低,adb 1.0.32版本不支持安卓8.x版本,更新adb版本后正常 Install homebrew ruby -e "$(curl -fsS ...