Java Solution


  • 0
    M
    public class Solution {
        public int wiggleMaxLength(int[] nums) {
            if (nums.length < 2) {
                return nums.length;
            }
            Boolean isGreater = null;
            int count = 1;
            for (int i = 1; i < nums.length; i++) {
                if ((isGreater == null || isGreater) && nums[i] < nums[i - 1]) {
                    count++;
                    isGreater = false;
                }
                else if ((isGreater == null || !isGreater) && nums[i] > nums[i - 1]) {
                    count++;
                    isGreater = true;
                }
            }
            return count;
        }
    }

Log in to reply
 

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