Another Java solution


  • 0
    L
        public int wiggleMaxLength(int[] nums) {
            if (nums == null || nums.length == 0)
                return 0;
                
            int lastNum = nums[0];
            int maxLen = 1;
            int negative = 0;
            
            for (int i = 1; i < nums.length; i++)
            {
                int diff = nums[i] - lastNum;
                if (negative == 0)
                {
                     if (diff > 0) negative = 1;
                     else if (diff < 0) negative = -1;
                     else negative = 0;
                }
                
                if (diff * negative > 0)
                {
                    maxLen++;
                    negative = -negative;
                }
                lastNum = nums[i]; 
            }
            
            return maxLen;
        }
    

Log in to reply
 

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