Simple, clean iterative java solution


  • 0
    G
        public int wiggleMaxLength(int[] nums) {
            if( nums == null || nums.length == 0 ) return 0;
            Boolean asc = null;
            int last = nums[0], count = 1;
            for( int i = 1; i < nums.length; i++ ) {
                if( nums[i] != last && (asc == null || (nums[i] > last) == asc) ) {
                    count++;
                    asc = !(nums[i] > last);
                }
                last = nums[i];
            }
            return count;
        }

Log in to reply
 

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