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 ...
随机推荐
- Teamviewer显示“未就绪,请检查您的连接”解决办法
打开TeamViewer一直提示“未就绪,请检查您的连接”,一直会弹出一个框提示检查网路设置什么. 解决办法:修改DNS为114.114.114.114,然后TeamViewer就显示网络正常. 为什 ...
- 资料汇总_Gitlab使用记录
1)搭建并配置本地GitLab服务器教程 https://www.cnblogs.com/yinkemeng/p/10144782.html 2)记一次为gitlab启用CI的过程 https://w ...
- C#——零散学习
C#——零散学习0 //控制台输入字符串,转化为int,double,float等数值类型: //Convert.ToXXX32();函数. Convert.ToInt32(); //把字符串转换为i ...
- Q-Q图和P-P图
一. QQ图 分位数图示法(Quantile Quantile Plot,简称 Q-Q 图) 统计学里Q-Q图(Q代表分位数)是一个概率图,用图形的方式比较两个概率分布,把他们 ...
- springmvc 注解二
@SessionAttributes @sessionattributes注解应用到Controller上面,可以将Model中的属性同步到session作用域当中. SessionAttribute ...
- AngularJS $http用法总结
最近由于项目需要,在研究AngularJS $http的用法,查了很多资料,发现貌似没有一篇内容可以完整的满足我对$http的基本了解,为了下次方便自己查找,所以特意把最近查到的一些资料和自己的理解记 ...
- el-table单元格样式更改
前几天遇到一个关于el-table表格样式的问题一直没解决 因为在el-table-column加样式并不生效所以更改起来比较麻烦 后来了看来element官方文档和在一些关于此方面的博客,使用了一个 ...
- JAVA基础之设置随机成语验证码
package com.oracle; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import ja ...
- vue中keep-alive,include的指定页面缓存问题
做vue项目时,有时要在某些页面做缓存,而其它页面不要.比如:A:首页,B:获取所有订单页面,C:订单详情页面:从A(首页)进入 B(获取所有订单)时应该不缓存,B(所有订单)进入 C(订单详情)订单 ...
- 冠捷显示成功的信息化建设(MES应用案例)
企业介绍 冠捷科技集团是驰誉全球的大型高科技跨国企业,产品包括彩色显示器( CRT monitor ).液晶显示器( LCD monitor ).液晶电视( LCD-TV )与等离子电视( PDP ) ...