Very simple Java solution


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

  • 0
    L

    The variable tmp can be substituted by nums[length-1]


Log in to reply
 

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