五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(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的評論 (共 條)

分享到微博請遵守國家法律
漠河县| 高密市| 台中县| 武平县| 巨鹿县| 晴隆县| 馆陶县| 共和县| 会东县| 胶南市| 廊坊市| 安岳县| 金乡县| 湖南省| 铁力市| 富源县| 郑州市| 昌平区| 常德市| 五大连池市| 双流县| 桦川县| 宝兴县| 突泉县| 潮安县| 石林| 漯河市| 景东| 邹平县| 呼图壁县| 邵东县| 府谷县| 睢宁县| 滨海县| 广元市| 乌鲁木齐县| 宜兴市| 崇仁县| 新沂市| 平谷区| 托克逊县|