Java Solution: Constant Space + Linear Complexity


  • 0
    H
    public int removeDuplicates(int[] nums) 
    	{	
    		if(nums == null)
    			return 0;
    		
    		if(nums.length <= 1)
    			return nums.length;
    		
    		int len = 1;
    		int unique = 0;
    		for(int i = 1; i < nums.length; i++)
    		{
    			if(nums[i] != nums[unique])
    			{
    				len++;
    				nums[len-1] = nums[i];
    				unique = i;
    			}
    		}	
            return len;
        }
    

Log in to reply
 

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