码迷,mamicode.com
首页 > 其他好文 > 详细

wiggle-subsequence

时间:2016-08-01 15:26:23      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

// 参考了:https://discuss.leetcode.com/topic/51893/two-solutions-one-is-dp-the-other-is-greedy-8-lines
// 另有:https://discuss.leetcode.com/topic/51946/very-simple-java-solution-with-detail-explanation

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int up = 1, down = 1;
        if (nums.size() <= 1) {
            return nums.size();
        }
        for (int i=1; i<nums.size(); ++i) {
            if (nums[i] < nums[i-1]) {
                down = up + 1;
            }
            else if (nums[i] > nums[i-1]) {
                up = down + 1;
            }
        }
        return max(up, down);
    }
};

 

wiggle-subsequence

标签:

原文地址:http://www.cnblogs.com/charlesblc/p/5725632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!