摇摆序列(LeetCode 376)

[1,17,5,10,13,15,10,5,16,8]整体不是摇摆序列

[1,17,5,10,…]
[1,17,5,13,…]
[1,17,5,15,…]

#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
int wiggleMaxLength(vector<int> &nums) {
if(nums.size() < 2) {
return nums.size();
}
static const int BEGIN = 0;
static const int UP = 1;
static const int DOWN = 2;
int STATE = BEGIN;
int max_length = 1;	//初始为1
//从第二个开始扫描
for(int i = 1; i < nums.size(); i++) {
switch(STATE) {
case BEGIN:
if(nums[i] > nums[i-1]) {
STATE = UP;
max_length++;
}
else if(nums[i] < nums[i-1]) {
STATE = DOWN;
max_length++;
}
break;
case UP:
//状态改变
if(nums[i] < nums[i-1]) {
STATE = DOWN;
max_length++;
}
break;
case DOWN:
//状态改变
if(nums[i] > nums[i-1]) {
STATE = UP;
max_length++;
}
break;
}
}
return max_length;
}
};

int main(void) {
vector<int> v;
/*
v.push_back(1);
v.push_back(7);
v.push_back(4);
v.push_back(9);
v.push_back(2);
v.push_back(5);
*/

v.push_back(1);
v.push_back(17);
v.push_back(5);
v.push_back(10);
v.push_back(13);
v.push_back(15);
v.push_back(5);
v.push_back(16);
v.push_back(8);

Solution s;
int res = s.wiggleMaxLength(v);
cout<<res<<endl;
return 0;
}


09-30 252
01-26 196
12-13 207
12-22 135
05-06 518
12-26