国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Leetcode 2348. Number of Zero-Filled Subarrays

2023-03-21 16:29 作者:您是打尖兒還是住店呢  | 我要投稿

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

A?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

最終祭出了數(shù)學(xué)公式,,,當(dāng)有連續(xù)n個(gè)0的時(shí)候,判斷有多少個(gè)組合,就是n*(n+1)/2,然后累加,

因?yàn)樽詈笠粋€(gè)可能算不到,因?yàn)樽詈笠晃豢赡苓€是0,所以結(jié)束for循環(huán)的時(shí)候,還要加一下判斷。確認(rèn)此時(shí)cnt是否為0,不為0,還要加cnt*(cnt+1)/2;

最后返回結(jié)果即可。



Runtime:?6 ms, faster than?34.17%?of?Java?online submissions for?Number of Zero-Filled Subarrays.

Memory Usage:?60.2 MB, less than?20.69%?of?Java?online submissions for?Number of Zero-Filled Subarrays.


Leetcode 2348. Number of Zero-Filled Subarrays的評論 (共 條)

分享到微博請遵守國家法律
昭平县| 安化县| 长泰县| 西城区| 襄垣县| 陇西县| 黔西县| 新安县| 青浦区| 闻喜县| 乐都县| 团风县| 浙江省| 新营市| 固安县| 嵊泗县| 搜索| 夹江县| 五原县| 朔州市| 津市市| 贺兰县| 霞浦县| 静海县| 荣昌县| 温州市| 汕尾市| 泽州县| 秦皇岛市| 吴川市| 汉阴县| 辉南县| 冷水江市| 兰坪| 绥滨县| 南城县| 宾阳县| 温宿县| 南投市| 宜君县| 贡山|