[给定一个排序数组,在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成]

分析图示: 

图

/**
 * 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
 * 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
 * @author mtreellen
 * @create 2019-10-16-22:48
 */
public class RemoveDuplicates {
    public static void main(String[] args) {
        int[] arr = new int[]{0,0,1,1,1,2,2,3,3,4};
        System.out.println(removeDuplicates(arr));
    }
    public static int removeDuplicates(int[] nums) {
        if(nums == null || nums.length == 1){
            return nums.length;
        }
        int i = 0;
        int j = 1;
        for(j = 1; j < nums.length;){
            if(nums[i] == nums[j]){
                j++;
            }else{
                i++;
                nums[i] = nums[j];
                j++;
            }
        }
        return i + 1;
    }
}
发布了91 篇原创文章 · 获赞 193 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44057443/article/details/102596556