Simple Java Solution


  • 0
    S
    public class Solution {
        public int wiggleMaxLength(int[] nums) {
            if(nums.length <= 1) return nums.length;
           int sign = 0;
           int max = 1;
           int keep = nums[1];
           if(nums[1] > nums[0]) {
               sign = 1;
               max++;
           }
           if(nums[1] < nums[0]) {
               sign = -1;
               max++;
           }
           for(int i = 2; i < nums.length; i++){
               if(sign == 1){
                   if(nums[i] > nums[i - 1]){
                       keep = nums[i];
                   }else if(nums[i] < nums[i - 1]){
                       keep = nums[i];
                       sign = -1;
                       max++;
                   }
               }else if(sign == -1){
                   if(nums[i] < nums[i - 1]){
                       keep = nums[i];
                   }else if(nums[i] > nums[i - 1]){
                       keep = nums[i];
                       sign = 1;
                       max++;
                   }
               }else if(sign == 0){
                   if(nums[i] < nums[i - 1]){
                       keep = nums[i];
                       sign = -1;
                       max++;
                   }else if(nums[i] > nums[i - 1]){
                       keep = nums[i];
                       sign = 1;
                       max++;
                   }
               }
           }
           return max;
        }
    }
    

Log in to reply
 

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