1. 程式人生 > >[leetcode]26. Remove Duplicates from Sorted Arrayde

[leetcode]26. Remove Duplicates from Sorted Arrayde

要注意i<nums.length,防止【1,4,4,4】越界

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