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. Pressed状态和clickable,duplicateParentState的关系

    做Android开发的人都用过Selector,可以方便的实现View在不同状态下的背景.不过,相信大部分开发者遇到过和我一样的问题,本文会从源码角度,解释这些问题. 首先,这里简单描述一下,我遇到的 ...

  2. Microsoft office(1)分页符和分节符

    Microsoft office下的页面布局中的分页符和分节符的区别: 分页符:标记一页的终止并开始下一页的点 分节符:插入分节符并在下一页开始新节 一般情况下,分节符在分页符外围,分节符一般是各种格 ...

  3. golangWEB框架gin学习之获取post参数

    原文地址:http://www.niu12.com/article/41 package main import ( "fmt" "github.com/gin-goni ...

  4. cs-HtmlHelpers

    ylbtech-Unitity: cs-HtmlHelpers PagingHelpers.cs  SelectInputHelpers.cs TreeHelpers.cs 1.A,效果图返回顶部   ...

  5. IP地址后面斜杠加具体数字详解

    其实这种形式就是用CIDR(无类别域间路由选择,Classless and Subnet Address Extensions and Supernetting))的形式表示的一个网段,或者说子网. ...

  6. Hyper-V Tools for win7

    http://download.microsoft.com/download/C/1/C/C1CA233D-CA1A-4C4D-8240-B4AFC0FD3433/Windows6.1-KB95883 ...

  7. node.js开发平台

    1.EDP:基于Node.JS与NPM的企业级开发平台 什么是EDP? EDP是一个基于Node.JS与NPM的企业级前端应用的开发平台.主要通过命令行的方式使用.EDP提供了前端应用开发时经常使用的 ...

  8. zoj 1100 - Mondriaan&#39;s Dream

    题目:在m*n的地板上铺上同样的1*2的地板砖,问有多少种铺法. 分析:dp,组合,计数.经典dp问题,状态压缩. 状态:设f(i,j)为前i-1行铺满,第i行铺的状态的位表示为j时的铺砖种类数: 转 ...

  9. Unity 编辑器扩展 场景视图内控制对象

    http://blog.csdn.net/akof1314/article/details/38129031 假设有一个敌人生成器类,其中有个属性range用来表示敌人生成的范围区域大小,那么可以用O ...

  10. Web service--百度百科

    Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...