My 1ms O(n) time O(1) space Java solution, easy to understand


  • 0
    M
    public class Solution {
        public int removeDuplicates(int[] nums) {
            if (nums.length < 2) {
                return nums.length;
            }
            int end = 0;
            for(int i = 1; i<nums.length;i++) {
                if (nums[end] < nums[i]) {
                    end++;
                    nums[end] = nums[i];
                } 
            }
            return end+1;
        }
    }

Log in to reply
 

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