10-line java solution


  • 0
    L
    public int wiggleMaxLength(int[] nums) {
        if(nums.length <= 1) return nums.length;
        int count = 1;
        for(int i = 1, diff = 0; i < nums.length; i++){
            int curDiff = nums[i] - nums[i - 1];
            if(diff == 0 && curDiff != 0 || curDiff != 0 && (curDiff ^ diff) < 0){
                count++;
                diff = curDiff;
            }
        }
        return count;
    }

Log in to reply
 

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