changgyhub / leetcode_101

LeetCode 101:力扣刷题指南
8.57k stars 1.16k forks source link

P42 413 Arithmetic Slices 解法有误 #76

Closed hkulyc closed 1 month ago

hkulyc commented 2 years ago
int numberOfArithmeticSlices(vector<int>& nums) {
    int n = nums.size();
    if (n < 3) return 0;
    vector<int> dp(n, 0);
    for (int i = 2; i < n; ++i) {
       if (nums[i] - nums[i-1] == nums[i-1] - nums[i-2]) {
           dp[i] = dp[i-1] + 1;
       }
}
    return accumulate(dp.begin(), dp.end(), 0);
}

参考解法最后一行只适用于整个数组都是等差数列的情况,但不适用于:

[1,2,3,5,4,5,6]

这种情况。

changgyhub commented 2 years ago

您再跑一下?您给的test case我测了一下是过的。