***Leetcode 376. Wiggle Subsequence

Posted Z-Pilgrim

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了***Leetcode 376. Wiggle Subsequence相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/wiggle-subsequence/description/

 

class Solution 
public:
    
    vector<int> filter(vector<int>& nums) 
        vector<int> ret;
        for (int i = 0; i < nums.size(); i++) 
            if (!ret.size() || nums[i] != ret.back() )
                ret.push_back( nums[i] );
        
        return ret;
    
    
    int wiggleMaxLength(vector<int>& ns) 
        vector<int> nums = filter(ns);
        if (nums.size() <= 1) return nums.size();
        if (nums.size() == 2) 
            return nums[1] == nums[0] ? 1 : 2;
        
        int last_type = nums[1] > nums[0],  last_idx = 1;
        int ans = 2;
        for (int i = 2; i < nums.size(); i++) 
            // printf( "i:%d last_type:%d nums[i]=%d nums[last_idx]=%d last_idx=%d\\n", i, last_type, nums[i], nums[last_idx], last_idx );
            if ( last_type ^ nums[i] > nums[last_idx] ) 
                ans ++;
                last_type =  (nums[i] > nums[last_idx]);
                last_idx = i;
             else 
                if (last_type && nums[i] > nums[last_idx]) 
                    last_idx = i;
                 else 
                    if (!last_type && nums[i] < nums[last_idx]) 
                        last_idx = i;
                    
                
            
            // printf( "===>>i:%d last_type:%d nums[i]=%d nums[last_idx]=%d last_idx=%d\\n", i, last_type, nums[i], nums[last_idx], last_idx );
        
        return ans;
    
;

 

以上是关于***Leetcode 376. Wiggle Subsequence的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 376. Wiggle Subsequence

Leetcode376. Wiggle Subsequence

***Leetcode 376. Wiggle Subsequence

LeetCode.376 - Wiggle Subsequence

LeetCode 376. Wiggle Subsequence 摆动子序列

leetcode 376Wiggle Subsequence