首页 > 其他分享 >【双指针】LeetCode 26. 删除有序数组中的重复项

【双指针】LeetCode 26. 删除有序数组中的重复项

时间:2023-01-31 09:24:02浏览次数:52  
标签:26 nums int 数组 LeetCode 指针

题目链接

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

思路

设定两个指针 ij,使用 j 遍历数组,将与前项不相等的元素放到 i 的位置。

代码

、class Solution {
    public int removeDuplicates(int[] nums) {
        int i = 1;
        int j = 1;

        while(j < nums.length){
            if(nums[j] != nums[j - 1]){
                nums[i] = nums[j];
                i++;
            }
            j++;
        }

        return i;
    }
}

标签:26,nums,int,数组,LeetCode,指针
From: https://www.cnblogs.com/shixuanliu/p/17077783.html

相关文章