easy Java O(n) solution ,0ms


  • 1
        public int wiggleMaxLength(int[] nums) {
        int size = nums.length;
            if(size<=1)return size;
            int temp = 1,sign = 0,i = 0;
                while(i<size-1){
                if(nums[i]>nums[i+1] && (sign==-1 || sign == 0)){
                    sign=1;
                    temp++;
                }
                if(nums[i]<nums[i+1] && (sign==1 || sign == 0)){
                    sign=-1;
                    temp++;
                }
                i++;
                }
            return temp;
        }

Log in to reply
 

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