share my java solution!


  • 0
    C
    
    public class Solution {
        public int wiggleMaxLength(int[] nums) {
            if(nums==null) return 0;
            if(nums.length<2) return nums.length;
            
            int p=1;
            int pre=nums[0];
            int i=0;
            while(i<=nums.length-1&&nums[i]==nums[0]){
                i++;
            }
            if(i==nums.length) return p;
            boolean increasing=(nums[i]>nums[0]) ? true:false;
            for(i=1;i<nums.length;i++){
                if(increasing){
                    if(nums[i]>pre){
                        p++;
                        pre=nums[i];
                        increasing=!increasing;
                    }else{
                         pre=nums[i];
                    }
                }else{
                    if(nums[i]<pre){
                        p++;
                        pre=nums[i];
                        increasing=!increasing;
                    }else{
                        pre=nums[i];
                    }
                }
            }
            
            return p;
        }
    }
    
    

Log in to reply
 

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