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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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 ...

  4. 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题是存储的当前累加和的 ...

  5. [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 ...

  6. [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 ...

  7. Subarray Sum Equals K LT560

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. iOS UILabel自定义行间距

    NSString *hintStr = @"输入材料标题搜索材料\n注:可根据材料序号直接搜索, 如TPO23"; CGSize size = [toolset returnTex ...

  2. Android 中AlarmManager升级4.2

  3. 使用.Net中的WeakDictionary — ConditionalWeakTable

    有的时候,我们需要给某些数据添加一些附加信息,一种常用的做法是使用一个Dictionary在填充这些附加信息如: var data = new Data();    var tag = new Tag ...

  4. 【JSP EL】el表达式判断是否为null

    后台程序放入Model中,从前台el表达式取出来非常方便,但是如果需要处理 当数据为null的时候,怎么办,不为null的时候,怎么办:这个怎么处理呢? <span class="us ...

  5. CMD_命令行

    一.bat执行一闪而过 最后一个end下一行,加PAUSE   二.cmd命令:不是内部或外部命令,也不是可运行的程序或批处理文件 解决: 需将路径先切换到system32下  cd c:\WINDO ...

  6. PostgreSQL on Linux 最佳部署手册

    安装常用包 # yum -y install coreutils glib2 lrzsz mpstat dstat sysstat e4fsprogs xfsprogs ntp readline-de ...

  7. CentOS release 6.6 (Final)如何安装firefox和chromium

    一.firefox的安装: 1. 安装remi源 rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8 ...

  8. 全文检索引擎[asp版]

    search.asp: <% set DM=server.CreateObject("DeepMap.HLL")pnn=0: wdd="": pnn=Re ...

  9. PHP正则表达式之快速学习法

    1.入门简介 简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或 ...

  10. Java遍历总结:for、for each和迭代器iterator

    一.for,for each和iterator用法和区别: 相同点:   三个都可以用来遍历数组和集合 不同点: 1.形式差别 //for的形式是 ;i<arr.size();i++){...} ...