统计「优美子数组」

2020-04-21  本文已影响0人  7赢月

题目描述

https://leetcode-cn.com/problems/count-number-of-nice-subarrays/

func numberOfSubarrays(nums []int, k int) int {
    var (
        t int
        c = make([]int, 0, len(nums))
    )
    c = append(c, -1)
    for p, v := range nums {
        if v%2 == 1 {
            c = append(c, p)
        }
    }
    c = append(c, len(nums))
    if len(c) < k {
        return 0
    }

    for i := 1; i+k < len(c); i++ {
        t += (c[i] - c[i-1]) * (c[i+k] - c[i+k-1])
    }
    return t
}

思路

及时总结和归纳吧!

上一篇下一篇

猜你喜欢

热点阅读