concise java solution 15 lines 0ms


  • 0
    W
    public int wiggleMaxLength(int[] nums) {
            int n = nums.length; if(n<=1) return n; //edge condition check
            int i=1;
            while(i<n&&nums[i]==nums[i-1])//remove the begining duplicate
                i++;
            if(i>=n-1) return 1;//if all deuplicate
            int count = 2;
            boolean b = (nums[i-1]>nums[i]);
            for(;i<n-1;i++){
                if((b&&nums[i]<nums[i+1])||(!b&&nums[i]>nums[i+1])){
                    b=!b;
                    count++;
                }
            }
            return count;
        }
    

Log in to reply
 

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