LeetCode 930. Binary Subarrays With Sum
原题链接在这里:https://leetcode.com/problems/binary-subarrays-with-sum/
题目:
In an array A of 0s and 1s, how many non-empty subarrays have sum S?
Example 1:
Input: A = [1,0,1,0,1], S = 2
Output: 4
Explanation:
The 4 subarrays are bolded below:
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
[1,0,1,0,1]
Note:
A.length <= 300000 <= S <= A.lengthA[i]is either0or1.
题解:
Maintian the count of sum from 0 to i.
If sum >= S, then res += count[sum-S].
Because, there must be t, t<i, sum from 0 to t is sum-S, from t to i is S. Then count of S is count of sum-S.
Time Complexity: O(n). n = A.length.
Space: O(n).
AC Java:
class Solution {
public int numSubarraysWithSum(int[] A, int S) {
if(A == null || A.length == 0){
return 0;
}
int [] count = new int[A.length+1];
count[0] = 1;
int sum = 0;
int res = 0;
for(int a : A){
sum += a;
if(sum >= S){
res += count[sum-S];
}
count[sum]++;
}
return res;
}
}
LeetCode 930. Binary Subarrays With Sum的更多相关文章
- [LeetCode] 930. Binary Subarrays With Sum 二元子数组之和
In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...
- 【LeetCode】930. Binary Subarrays With Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 字典 相似题目 参考资料 日期 题目地址: ...
- 108th LeetCode Weekly Contest Binary Subarrays With Sum
In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...
- [Swift]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum
In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...
- [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
随机推荐
- C/C++ 每日一题
超长正整数的相加,题目链接:https://www.nowcoder.com/practice/5821836e0ec140c1aa29510fd05f45fc?tpId #include<al ...
- SPOJ Qtree系列
Qtree1 将边权变为这条边连接的两个点中深度更深的点的点权,这样就可以变为带修改链上最大点权.直接树链剖分即可. 下面是一份C语言代码 #include<stdio.h> #inclu ...
- Maven聚合项目的创建
1.项目结构如下 步骤如下: 点击Finish 这里父项目需要加入如下的构建依赖: <dependencyManagement> <dependencies> <depe ...
- Kafka启用SASL_PLAINTEXT动态配置JAAS文件的几种方式
Kafka是广泛使用消息服务,很多情况下关于认证部分我都是默认的配置,也就是不需要用户名/密码,也不配置证书.在内网或者在项目组内部可以,但是设计的跨部门时一般处于安全考虑都需要加上认证,防止kafk ...
- Delphi中AssignFile函数
procedure TForm1.SaveLog(sFlag:string;MSG:string);var QF1:Textfile; ----声明文本文件类型 Qfiletmp,sP ...
- RStudio中安装factoextra包的问题
最近在做一个R语言的小作业,其中聚类分析部分需要用到factoextra安装包,在RStudio中输入install.packages("factoextra")之后,就一直出现“ ...
- python3 语法 数据类型
python3中 有6种标准数据类型 数字,字符串,列表,元祖,集合,字典
- P1349 广义斐波那契数列(矩阵乘法)
题目 P1349 广义斐波那契数列 解析 把普通的矩阵乘法求斐波那契数列改一改,随便一推就出来了 \[\begin{bmatrix}f_2\\f_1 \end{bmatrix}\begin{bmatr ...
- 【开发工具】- Myeclipse10.7破解方法
1.下载myeclipse 10,如果没有,可以使用链接:https://pan.baidu.com/s/1l9juqD4ALMuepVL6e5kgjA 密码:kpx6:当然时间久了可能链接失效,如有 ...
- jquery实现弹出层完美居中效果
代码如下: showDiv($("#pop"));function showDiv(obj){ $(obj).show(); center(obj); $(window).scro ...