水字数日更专用-移除元素Plus

2024-01-27  本文已影响0人  ButICare_b72d

package main.java.simple;

/**

class RemoveDuplicates2 {
public static int removeDuplicates(int[] nums) {
int left = 0;
int right = 0;
int tag = 0;
while (right < nums.length) {
if (nums[right] != nums[left]) {
left = right;
}
if (right - left < 2 || nums[right] != nums[left]) {
nums[tag] = nums[left];
tag++;
}
right++;
}
return tag;
}

public static void main(String[] args) {
    System.out.println(removeDuplicates(new int[]{1, 1, 1, 2, 2, 3}));
    System.out.println(removeDuplicates(new int[]{1}));
    System.out.println(removeDuplicates(new int[]{2, 3, 4}));
}

}

上一篇下一篇

猜你喜欢

热点阅读