JAVA very simple easy to understand solution


  • 0
    H
    public class Solution {
        public int wiggleMaxLength(int[] nums) {
            if (nums.length == 0 || nums.length == 1) {
    			return nums.length;
    		}
            int k=0;
            int res = 1;
            for(int i=0;i<nums.length-1;i++){
                int tmp = nums[i] - nums[i+1];
                if(k==0){
                    k=-tmp;
                }
                if((k<0 && tmp>0) || (k>0 && tmp<0)){
                    res++;
                    k=tmp;
                }
            }
            return res;
        }
    } 
    

Log in to reply
 

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