leetcode 26. 删除有序数组中的重复项

直接法

    public int removeDuplicates(int[] nums) {
        if (nums == null) {
            return -1;
        }
        boolean start = false;
        int nextIndex = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != nums[i - 1]) {
                if (!start) {
                    continue;
                } else {
                    nums[nextIndex++] = nums[i];
                }
            } else {
                if (!start) {
                    nextIndex = i;
                    start = true;
                }
            }
        }
        return start ? nextIndex : nums.length;
    }
posted @ 2023-07-04 16:05  jrjewljs  阅读(3)  评论(0编辑  收藏  举报