Golang | Leetcode Golang题解之第413题等差数列划分

题目:

题解:

Go 复制代码
func numberOfArithmeticSlices(nums []int) (ans int) {
    n := len(nums)
    if n == 1 {
        return
    }

    d, t := nums[0]-nums[1], 0
    // 因为等差数列的长度至少为 3,所以可以从 i=2 开始枚举
    for i := 2; i < n; i++ {
        if nums[i-1]-nums[i] == d {
            t++
        } else {
            d, t = nums[i-1]-nums[i], 0
        }
        ans += t
    }
    return
}
相关推荐
利威尔xu1 小时前
Go map 底层崩溃:3 个 goroutine 并发写同一张 map,fatal error 崩掉整个服务
golang·源码·map·并发·后端开发
Rain的Java大神之路2 小时前
LeetCode 37 解数独面试攻略:回溯 + 位掩码 + MRV,从暴力到毫秒级求解
java·后端·leetcode
Pniubi3 小时前
力扣73 矩阵置零
算法·leetcode·矩阵
文丑颜不良啊19 小时前
LeetCode2261~2280题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2241~2260题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2321~2340题目解析
java·leetcode
利威尔xu1 天前
2026-11-08-Go-defer-panic-recover面试
面试·golang
文丑颜不良啊2 天前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z2 天前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
文丑颜不良啊2 天前
LeetCode2161~2180题目解析
java·leetcode