java 12ms space const, time O(n)


  • 0
    A
     public int removeDuplicates(int[] nums) {
        int length = 1;
        int currSpace = 0;
        int currIdx = 1;
        while (currIdx < nums.length){
            if (nums[currIdx] == nums[currSpace]){
                currIdx++;
            }
            else {
                currSpace++;
                nums[currSpace] = nums[currIdx];
                currIdx++;
                length++;
            }
        }
        return length;
    }

Log in to reply
 

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