Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P42 413 Arithmetic Slices 解法有误 #76

Open
hkulyc opened this issue Aug 3, 2022 · 1 comment
Open

P42 413 Arithmetic Slices 解法有误 #76

hkulyc opened this issue Aug 3, 2022 · 1 comment

Comments

@hkulyc
Copy link

hkulyc commented Aug 3, 2022

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants