LeetCode 930. Binary Subarrays With Sum
原题链接在这里:https://leetcode.com/problems/binary-subarrays-with-sum/
题目:
In an array A
of 0
s and 1
s, 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 <= 30000
0 <= S <= A.length
A[i]
is either0
or1
.
题解:
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 ...
随机推荐
- MyBatis参数条件查询传入的值为0时的判断
MyBatis条件查询对字段判断是否为空一般为: <if test="testValue!=null and testValue != ''"> and test_va ...
- 单选 textarea 赋初值
自闭合标签赋初值用$().val(),比如:<input type="text" name="text" value="123"> ...
- JAVA知识点总结篇(一)
JVM(Java Virtual Machine):源文件->编译器->字节码文件->解释器->程序: JDK:Java Development Kit,Java开发工具包: ...
- SAS学习笔记1
数据采样 简单随机抽样,从sashelp数据集中air数据文件中选取30个数 数据探索 数字特征的探索:均值.频数.最大值.最小值.众数.中位数.方差.标准差 数字分布的探索:是否服从正态分布 连续型 ...
- Jmeter相关参数
一.线程组 线程组主要包含三个参数:线程数.准备时长(Ramp-Up Period(in seconds)).循环次数. 线程数:虚拟用户数.一个虚拟用户占用一个进程或线程.设置多少虚拟用户数在这里也 ...
- 获取当前URL地址和$_GET获取参数
用这个方法,可以在不使用$_get[]就可以获取get传过来的参数.还可以获取当前的URL public function getCurrentUrl() { $pageURL = 'http'; i ...
- Spring-Boot之Admin服务监控-9
一.Spring Boot Admin用于管理和监控一个或者多个Spring Boot程序.Spring Boot Admin分为Server端和Client 端,Client端可以通过向Http S ...
- Tomcat组件梳理—Service组件
Tomcat组件梳理-Service组件 1.组件定义 Tomcat中只有一个Server,一个Server可以用多个Service,一个Service可以有多个Connector和一个Contain ...
- 基于xilinx Zynq UltraScale MPSoC平台的核心板及开发板介绍-米尔科技
近日,米尔科技推出国内首款基于xilinx Zynq UltraScale+MPSoC 平台的核心板及开发板.其优势主要有:采用16纳米制程,相比Znyq7000系列每瓦性能提升5倍,且单芯片融合4核 ...
- JS中的七大数据类型
js中有7种数据类型,包括五种基本数据类型(Number,String,Boolean,Undefined,Null),和一种复杂数据类型(Object)以及es6语法新增的Symbol数据类型 es ...