Simple Java Solution


  • 0
    E

    Hey guys, this is my simple java version solution.

            if (nums.length <= 1) return nums.length;
            int index = 1, j = 0;
            while (j + 1 < nums.length && nums[j] <= nums[j + 1]) j++;
            if (nums[j] != nums[0]) {
                nums[index++] = nums[j];
            } 
            for (; j < nums.length; j++) {
                while (j + 1 < nums.length && nums[j] >= nums[j + 1]) j++;
                if (nums[j] != nums[index - 1])
                    nums[index++] = nums[j];
                j++;
                if (j == nums.length) break;
                while (j + 1 < nums.length && nums[j] <= nums[j + 1]) j++;
                if (nums[j] != nums[index - 1])
                    nums[index++] = nums[j];
            }
            return index;
        }

Log in to reply
 

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