My ugly codes


  • 0
    C

    I want to show my ugly codes.

    public class Solution {
        public int wiggleMaxLength(int[] nums){
            int count = 0;
            if(nums.length <=1){
                return nums.length;
            }
    
            int sign = 1;
            if(nums[1]-nums[0] > 0){
                sign = 1;
                count = 2;
            }else if(nums[1]-nums[0] <0){
                sign = -1;
                count = 2;
            }else{
                count = 1;
                sign = 0;
            }
    
            for(int i = 1;i<nums.length-1;i++){
                while(sign ==1 && i<nums.length-1 && nums[i+1] > nums[i] ){
                    i++;
                }
                while(sign == 0 && i<nums.length-1 && nums[i+1] == nums[i]){
                    i++;
                }
                while((sign ==-1 ) && i<nums.length-1 && nums[i+1] < nums[i]){
                    i++;
                }
                if(i == nums.length-1){
                    break;
                }
                if((sign ==1 || sign == 0) && nums[i+1] < nums[i]){
                    count++;
                    sign = -1;
                }
                if((sign ==-1 || sign == 0) &&  nums[i+1] > nums[i]){
                    sign = 1;
                    count++;
                }
            }
            return count;
        }
    }
    

Log in to reply
 

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