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 ...
随机推荐
- delphi杀进程的两种方式
delphi杀进程的两种方式 uint unit Tlhelp32; 第一种:比较简单,根据标题,找到窗口,再找到进程,杀死进程 procedure KillProgram(WindowTitle : ...
- 【转】python中文转换url编码
今天要处理百度贴吧的东西.想要做一个关键词的list,每次需要时,直接添加 到list里面就可以了.但是添加到list里面是中文的情况(比如‘丽江’),url的地址编码却是'%E4%B8%BD%E6% ...
- spring利用后置处理器初始化bean属性
spring利用后置处理器初始化bean属性 参考:http://blog.csdn.net/heyutao007/article/details/50326793 @Configurable @En ...
- 在对方电脑建立IPC连接, 利用IPC$入侵 运行木马
第一大步: IPC漏洞的建立 1)在目标主机上设置组策略:開始->执行-〉gpedit.msc 2)计算机配置->windows配置-〉本地策略-〉安全选项 3)在安全选项中, 将网络訪 ...
- 火车票抢票API 根据乘客的车次与座席要求快速订票出票
火车票抢票API 根据乘客的车次与座席要求快速订票出票:https://www.juhe.cn/docs/api/id/257 1.站站查询 接口地址:http://v.juhe.cn/grabTic ...
- Django——20141014深入理解Django HttpRequest HttpResponse的类和实例
深入理解Django HttpRequest HttpResponse的类和实例 了解META选项 了解中间件 理清所有模板传输模板变量的方式,并作出选择 Django模板系统:如何利用Django模 ...
- JavaScript内存示意图
一.JavaScript内存示意图 二.js原理 1.js执行规则 (1)先定义 定义变量.定义函数.顺序:自上而下. (2)后执行 除了定义外的代码都是执行代码.顺序:自上而下. 2.动态开辟内存 ...
- Python实时语音识别控制
代码地址如下:http://www.demodashi.com/demo/12946.html Python实时语音识别控制 概述 本文中的语音识别功能采用 百度语音识别库 ,首先利用 PyAudio ...
- 使用history.pushState()和popstate事件实现AJAX的前进、后退功能
上一篇文章中.我们使用location.hash来模拟ajax的前进后退功能.使用location.hash存在以下几个问题: 1.使用location.hash会导致地址栏的url发生变化.用户体验 ...
- Hibernate单向“多对一”关联
1. 基于外键关联的单向“多对一”关联是最常见的单向关联其中指定many-to-one的unique="true",为单向“一对一”,不指定就是单向“多对一” <class ...