Java O(n) So Simple


  • 0
    R

    public int wiggleMaxLength(int[] nums) {

        int count = 1,len = nums.length;
        if(len < 2) return len;
        boolean flag = nums[1]>nums[0];
        for(int i = 0;i<len-1;i++){
            if(nums[i+1]==nums[i]) continue;
             if( nums[i+1]>nums[i] == flag){
                 flag = !flag;
                 count++;
             }
             
        }
        return count;
    }

  • 0
    I

    This is not right.
    Input:
    [1,1,7,4,9,2,5]
    Output: 5 Expected: 6


Log in to reply
 

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