LeetCode560. Subarray Sum Equals K
Description
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 = 2
Output: 2
Note:
The length of the array is in range [1, 20,000].
The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
my program
思路:创建一个数组tmp,用来储存前n项的和。
第一层循环:用来计算每一个前i项的和,判断如果前i项和等于k,则结果res加一;
内层循环:遍历tmp的前i项,判断如果(前i项和-前j项和)== k,则结果res加一;
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
vector<int> tmp = nums;
int res = 0;
if (nums[0] == k) res=1;
for (int i = 1; i<nums.size(); i++) {
tmp[i] += tmp[i-1];
if (tmp[i] == k) res++;
for (int j = 0; j < i; j++) {
if (tmp[i] - tmp[j] == k) res++;
}
}
return res;
}
};
Submission Details
80 / 80 test cases passed.
Status: Accepted
Runtime: 369 ms
虽然AC了,但是runtime很高,于是思考是否有更有的算法。
暴力解法
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int res = 0, n = nums.size();
for (int i = 0; i < n; ++i) {
int sum = nums[i];
if (sum == k) ++res;
for (int j = i + 1; j < n; ++j) {
sum += nums[j];
if (sum == k) ++res;
}
}
return res;
}
};
Submission Details
80 / 80 test cases passed.
Status: Accepted
Runtime: 538 ms
两种解法的时间复杂度均为O(n2),类似
哈希表解法
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int res = 0;
map<int, int> tmp{{0,1}};
int sum = 0;
for (int i = 0; i < nums.size(); i++) {
sum += nums[i];
res += tmp[sum - k];
++tmp[sum];
}
return res;
}
};
非常巧妙,时间复杂度仅为O(n),相当于仅仅遍历了数组。
LeetCode560. Subarray Sum Equals K的更多相关文章
- 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 ...
- 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题是存储的当前累加和的 ...
- [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] Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- 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 ...
随机推荐
- iOS开发经验——点击屏幕空白处退出键盘
一种比较简单的点击屏幕空白处退出键盘的方法: 在ViewController中加入如下代码: 1: -(void)touchesBegan:(NSSet *)touches withEve ...
- 文件描述符与socket连接
每个进程开启一个soeket连接,都会占用一个文件描述符. 1. 概述 在Linux系统中一切皆可以看成是文件,文件又可分为:普通文件.目录文件.链接文件和设备文件. 文件 ...
- html5页面中 触发 拨打电话、发短信 的方式
<a href="tel:18688888888">拨号</a> <a href="sms:18688888888">发短信 ...
- PhantomJS 基础及示例
腾讯云技术社区-掘金主页持续为大家呈现云计算技术文章,欢迎大家关注! 作者:link 概述 PhantomJS is a headless WebKit scriptable with a JavaS ...
- nodeJs常用API
1.url (1)url.parse返回url对象的各种参数 url.parse(url,true/false,true/false);//默认url.parse(url,false,false); ...
- 在Oracle Enterprise Linux R5U7上安装Oracle 11gr2数据库
折腾了好几次,经验是: 包的安装 在安装包里,需要把开发方面的安装包都装上. 另外安装完成后,需要安装的包包括: cd /media/cdrom/Server rpm -Uvh binutils-2. ...
- sql where 1=1和 0=1 的作用(多条件查询错误的问题)
where 1=1; 这个条件始终为True,在不定数量查询条件情况下.1=1能够非常方便的规范语句. 一.不用where 1=1 在多条件查询中的困扰 举个样例,假设您做查询页面,而且.可查询的 ...
- SharePoint 2013 项目部署
SharePoint 2013 项目部署 本人刚接触sharepoint不久,是个小菜鸟,而且上手版本是2013,对10和07版也没有太多的了解.最近由于项目需要本人磕磕碰碰部署了sharepoint ...
- js 上传图片进行回显
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Retrofit全攻略——基础篇
实际开发过程中一般都会选择一些网络框架提升开发效率.随着Google对HttpClient 摒弃和Volley框架的逐渐没落.OkHttp開始异军突起.而Retrofit则对OkHttp进行了强制依赖 ...