26. 删除排序数组中的重复项(快慢指针)

155 阅读1分钟

地址:leetcode-cn.com/problems/re…

class Solution {
    public int removeDuplicates(int[] nums) {
         if (nums.length == 0) {
            return 0;
        }
        int i = 0;
        for (int j = 1; j < nums.length; j++) {
            if (nums[j] != nums[i]) {
                i++;
                nums[i] = nums[j];
            }
        }
        return i + 1;
    }
}