其他分享
首页 > 其他分享> > 删除有序数组中的重复项

删除有序数组中的重复项

作者:互联网

python 版本:遍历列表原地删除

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        pre = None;
        i = 0;
        while i<len(nums):
            if pre == nums[i]:
                del(nums[i])
            else:
                pre = nums[i]
                i = i+1
        return i;

java 版本:设置两个指针,i指针指向补充重复的,j指针向后移动

class Solution {
    public int removeDuplicates(int[] nums) {
       
        if(nums.length == 0) return 0;
           
        int i = 0;
       
        for(int j :nums){
           
           if(nums[i] == j) continue;  
            i++;
            nums[i] = j;
        }
        return i+1;
    }
}

标签:pre,return,数组,删除,nums,int,有序,removeDuplicates,指针
来源: https://www.cnblogs.com/CoderRdf/p/12446917.html