原题链接在这里: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:

  1. A.length <= 30000
  2. 0 <= S <= A.length
  3. A[i] is either 0 or 1.

题解:

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的更多相关文章

  1. [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, ...

  2. 【LeetCode】930. Binary Subarrays With Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 字典 相似题目 参考资料 日期 题目地址: ...

  3. 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, ...

  4. [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, ...

  5. [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 ...

  6. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  7. 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 ...

  8. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  9. 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 ...

随机推荐

  1. python学习-30 总结

    小结 1.map函数: 处理序列中的每个元素,得到结果是一个‘列表’,该‘列表’元素个数及位置与原来一样 2.filter:遍历序列中的每个元素,判断每个元素得到的布尔值,如果是True则留下来,例如 ...

  2. day56——http协议、MVC和MTV框架模式、django下载安装、url路由分发

    day56 昨日复习 今日内容 HTTP协议 网页:https://www.cnblogs.com/clschao/articles/9230431.html 老师整理的重点 老师整理的重点 请求信息 ...

  3. C 猜猜猜😀文字小游戏

    前言 - 随机性 随机数生成 - https://zh.wikipedia.org/wiki/%E9%9A%8F%E6%9C%BA%E6%95%B0%E7%94%9F%E6%88%90 没啥事情, 写 ...

  4. 【Maven基础入门】01 Maven的安装与环境变量的配置

    写在前面: Mavne,作为一个优秀的项目构建工具,虽说我们平时在使用的时候或多或少的会使用到它,但了解仅限于它能构建项目,然后其他的就不知道了. 以及仓库.POM父类文件.等等. 工欲善其事,必先利 ...

  5. linux安装mysql后报错启动不了Starting MySQL. ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).

    今天安装完Mysql后,开启发生了错误: 2.打开错误信息文件,查看错误原因是:Plugin 'FEDERATED' is disabled. /usr/sbin/mysqld: Table 'mys ...

  6. 【转载】C#中double.TryParse方法和double.Parse方法的异同之处

    在C#编程过程中,double.TryParse方法和double.Parse方法都可以将字符串string转换为double类型,但两者还是有区别,最重要的区别在于double.TryParse方法 ...

  7. Caffeine用法

    Caffeine是使用Java8对Guava缓存的重写版本,在Spring Boot 2.0中将取代Guava.如果出现Caffeine,CaffeineCacheManager将会自动配置.使用sp ...

  8. 在DoNetMVC中使用控制反转和依赖注入【DI】

    本次是在MVC5中使用Autofac 第一步:程序包管理器控制台 Install-Package Autofac.MVC5 引入nuget包 这样成功之后,会在引用中出现两个DLL,分别是Autofa ...

  9. 身份证验证PHP类

    PHP根据身份证号,自动获取对应的星座函数,然后自动返回对应的星座,自动返回性别,判断是否成年 <?php class IdcardAction extends Action{ // PHP根据 ...

  10. python(读取excel操作-xlrd模块)

    一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 或者在cmd窗口  pip install  ...