my solution is the most simple and efficient.


  • 0
    H
    class Solution {
    public:
        int wiggleMaxLength(vector<int>& nums) {
            if (nums.empty())
    			return 0;
    		int length = 1, i = 1;
    		while (i < nums.size() && nums[i] == nums[0])
    			i++;
    		bool flag = i < nums.size() && nums[i] > nums[0];
    		while (i < nums.size())
    		{
    			length++;
    			i++;
    			if (flag)
    				while (i < nums.size() && nums[i] >= nums[i - 1])	i++;
    			else
    				while (i < nums.size() && nums[i] <= nums[i - 1])	i++;
    			flag = !flag;				
    		}
    		return length;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.