本文主要是介绍C++ | Leetcode C++题解之第376题摆动序列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution {
public:int wiggleMaxLength(vector<int>& nums) {int n = nums.size();if (n < 2) {return n;}int prevdiff = nums[1] - nums[0];int ret = prevdiff != 0 ? 2 : 1;for (int i = 2; i < n; i++) {int diff = nums[i] - nums[i - 1];if ((diff > 0 && prevdiff <= 0) || (diff < 0 && prevdiff >= 0)) {ret++;prevdiff = diff;}}return ret;}
};
这篇关于C++ | Leetcode C++题解之第376题摆动序列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!