0ms C solution


  • 0
    V

    The idea is to iterate the array, use a state variable to store last valid (that follows the wiggling pattern) difference's state (1 for positive, -1 for negative), and if current difference does not wiggle, continue to next.

    int wiggleMaxLength(int* nums, int numsSize)
    {
        if (numsSize < 2)
            return numsSize;
    
        int i, len, last;
    
        for (i = len = 1, last = 0; i < numsSize; i++) {
            if (nums[i] > nums[i - 1])
                if (last == 1)
                    ;
                else
                    len++, last = 1;
            else if (nums[i] < nums[i - 1])
                if (last == -1)
                    ;
                else
                    len++, last = -1;
            else
                ;
        }
    
        return len;
    }
    

Log in to reply
 

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