My java solution with O(n)


  • 0
    P

    '''
    public class Solution {
    public int removeDuplicates(int[] nums) {
    int i=0,j=0;
    while(i<nums.length-1){
    if(nums[i]==nums[i+1]){
    i++;
    }else if(nums[i]!=nums[i+1]&&nums[j]==nums[i+1]){
    i++;
    }else if(nums[i]!=nums[i+1]&&nums[j]!=nums[i+1]){
    j=j+1;
    nums[j]=nums[i+1];
    }
    }
    return j+1;
    }
    }
    '''


Log in to reply
 

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