LeetCode - Subarray sum equals k
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].
解法一:利用sum之间的差暴力搜索
class Solution {
public int subarraySum(int[] nums, int k) {
if (nums == null || nums.length == 0)
return 0;
int len = nums.length;
int[] sum = new int[len+1];
for (int i=0; i<len; i++)
sum[i+1] = sum[i] + nums[i];
int cnt = 0;
for (int i=0; i<len; i++) {
for (int j=i+1; j<=len; j++) {
if (sum[j] - sum[i] == k)
cnt ++;
}
}
return cnt;
}
}
解法二:利用map建立sum和出现次数cnt的映射关系
class Solution {
public int subarraySum(int[] nums, int k) {
if (nums == null || nums.length == 0)
return 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
int sum = 0, cnt = 0;
for (int num : nums) {
sum += num;
cnt += map.getOrDefault(sum-k, 0);
map.put(sum, map.getOrDefault(sum, 0)+1);
}
return cnt;
}
}
第二种解法耗时更少

LeetCode - Subarray sum equals k的更多相关文章
- [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] 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题是存储的当前累加和的 ...
- 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] 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 (子数组之和等于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 ...
随机推荐
- hiredis 使用 linux c++
1.linux下如何安装hiredis 1)下载地址 https://github.com/redis/hiredis 2)编译和安装 解压后的文件夹执行 make;make install; 3) ...
- 如何修改IE浏览器的User-Agent用户代理字符串信息
每款浏览器都有一个专属的 User-Agent 字符串信息, 通过 User-Agent 网站可以检测用户所使用的浏览器版本.某些网站为了让用户获得更好的浏览体验,通过检测用户的浏览器版本,以确认用户 ...
- Google C++ 单元测试 GTest
from : http://www.cnblogs.com/jycboy/p/6057677.html 一.设置一个新的测试项目 在用google test写测试项目之前,需要先编译gtest到lib ...
- Error opening terminal: xterm-256color
在使用gdb调试linux内核时,提示如下错误: arm-none-linux-gnueabi-gdb --tui vmlinux Error opening terminal: xterm-256c ...
- Win10系统的SurfacePro4的触摸笔如何使用
初次使用需要配对,微软的触摸笔是蓝牙配对的,打开平板的蓝牙,长按触摸笔后面的按钮,触摸笔会闪烁小灯,平板会提示配对准备已就绪 点击配对之后,提示已连接 可以按下触摸笔后面的按钮,一键打开One ...
- Object 标签遮挡 Div 显示
最近在使用 Object 时,就是播放视频的 Object 标签遮挡住其他 div 标签,不能正常显示. 出现这种现象的原因: object 标签不在 dom 文档流里面,浏览器在解析的时候先把 ob ...
- 学习ReentrantLock
1. 使用ReentrantLock import java.util.concurrent.locks.ReentrantLock; public class TestReentrantLock { ...
- iis url重写
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...
- 前端切图实战(PSD设计稿转化为前端)
课程来源:https://www.imooc.com/learn/668 一:读设计稿 划分:头部.尾部.公共部分.大概分多少块.logo的重用.列表有哪些.各部分用什么技术实现等等. 二:建立项目目 ...
- js传递数组
注意:数组不能直接作为参数传给后台,需要先用JSON处理成数组字符串,传到后台后再转回数组. var arr_str = JSON.stringify(array)