首页 > 其他分享 >[LeetCode] 2348. Number of Zero-Filled Subarrays

[LeetCode] 2348. Number of Zero-Filled Subarrays

时间:2023-03-21 11:14:45浏览次数:55  
标签:count return nums Subarrays There Number 2348 数组 subarray

Given an integer array nums, return the number of subarrays filled with 0.

subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [1,3,0,0,2,0,0,4]
Output: 6
Explanation: 
There are 4 occurrences of [0] as a subarray.
There are 2 occurrences of [0,0] as a subarray.
There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.

Example 2:

Input: nums = [0,0,0,2,0,0]
Output: 9
Explanation:
There are 5 occurrences of [0] as a subarray.
There are 3 occurrences of [0,0] as a subarray.
There is 1 occurrence of [0,0,0] as a subarray.
There is no occurrence of a subarray with a size more than 3 filled with 0. Therefore, we return 9.

Example 3:

Input: nums = [2,10,2019]
Output: 0
Explanation: There is no subarray filled with 0. Therefore, we return 0.

Constraints:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109

全 0 子数组的数目。

给你一个整数数组 nums ,返回全部为 0 的 子数组 数目。

子数组 是一个数组中一段连续非空元素组成的序列。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/number-of-zero-filled-subarrays
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道数学题,也可以用动态规划做。我参考了这个帖子

具体的思路是如果我们当前位置上遇到的是一个 0,我们就把当前这个 0 当做子数组的结尾,来统计以当前这个 0 为结尾的符合题意的子数组有多少。举个例子,比如 [0, 0, 0, 0],我们设一个变量 count 记录当前遇到的连续的 0 的个数

当我们遇到第一个 0 的时候,count = 1, res += count

当我们遇到第二个 0 的时候,count = 2, res += count

这里 count 其实暗含了子数组的个数。

时间O(n)

空间O(1)

Java实现

 1 class Solution {
 2     public long zeroFilledSubarray(int[] nums) {
 3         long res = 0;
 4         int n = nums.length;
 5         int count = 0;
 6         for (int i = 0; i < n; i++) {
 7             if (nums[i] == 0) {
 8                 count++;
 9                 res += count;
10             } else {
11                 count = 0;
12             }
13         }
14         return res;
15     }
16 }

 

LeetCode 题目总结

标签:count,return,nums,Subarrays,There,Number,2348,数组,subarray
From: https://www.cnblogs.com/cnoodle/p/17239225.html

相关文章

  • 每天上一当系列之vue修饰符.number
    今天使用number修饰符去处理el-input的内容为数字做校验原本以为省事不少,没想到,为0开头无法输入第二位以后,并且输入的比较多的时候会出现Infinity很神奇,网上查了说是eleme......
  • Java: The number of bits of an integer is solved
    /***版权所有2022涂聚文有限公司*许可信息查看:*描述:1-100累加值但要求跳过所有包含有3的数*公用类库公共所需要用的操作函数或方法**历史版本:JDK......
  • cpp generate template number and fill array then order by quick sort
    //model/util.htemplate<typenameT>Tgen_random_num(Tmin,Tmax);voidgen_uint64(constint&len);voidgen_uint32(constint&len);templat......
  • Oracle SQL 常用的将varchar数据处理成number的正则
    OracleSQL常用的数据处理正则去除所有的空格replace(t.dxmz,chr(32),'')匹配纯数字regexp_like(t.zgbs,'^[[:digit:]]*$')含有字母regexp_like(t.dxmz,'[a-zA-Z]')......
  • [LeetCode] 1334. Find the City With the Smallest Number of Neighbors at a Thresh
    Thereare n citiesnumberedfrom 0 to n-1.Giventhearray edges where edges[i]=[fromi,toi,weighti] representsabidirectionalandweightededge......
  • React数字滚动组件 numbers-scroll
    数字滚动组件,也可以叫数字轮播组件,这个名字一听就是非常普通常见的组件,第一反应就是想找找网上大佬的东西顶礼膜拜一下,这一搜,还真是没找到趁手的╮(╯▽╰)╭。最近接了大......
  • spring的NumberFormat注解
    转载:https://blog.51cto.com/u_3631118/3121347https://blog.csdn.net/weixin_38192427/article/details/122270716spring除了@DateTimeFormat之外,还提供了一个@NumberFo......
  • 287. Find the Duplicate Number
    ##题目Givenanarraynumscontainingn+1integerswhereeachintegerisbetween1andn(inclusive),provethatatleastoneduplicatenumbermustexist.......
  • 1009. K-based Numbers
    1009.K-basedNumbershttps://acm.timus.ru/problem.aspx?space=1&num=1009 思路典型dp问题对于n位k位底的数,求不存在连续0出现的数目。设f(i)为i位,最后一位为......
  • SqlServer ROW_NUMBER() Over()分页
    --ROW_NUMBER()Over()分页,计算公式((@pageIndex-1)*@pageSize)+1And(@pageIndex*@pageSize)DECLARE@pageSizeINTDECLARE@pageIndexINT--第4页,每页显......