My 1ms java solution


  • 0
    L
    public class Solution {
    public int removeDuplicates(int[] nums) {
        if(nums.length==0) return 0;
        int index=0;
        for(int i=1;i<nums.length;i++){
            if(nums[i]!=nums[i-1]){
                index++;
                if(index!=i){//when index==i,we need not to move data,it can save time.
                  nums[index]=nums[i];  
                }
            }
        }
        return index+1;
    }
    

    }


Log in to reply
 

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