美文网首页
水字数日更专用-移除元素Plus

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

作者: ButICare_b72d | 来源:发表于2024-01-27 02:03 被阅读0次

    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}));
    }
    

    }

    相关文章

      网友评论

          本文标题:水字数日更专用-移除元素Plus

          本文链接:https://www.haomeiwen.com/subject/rkfiodtx.html