美文网首页
26. Remove Duplicates from Sorte

26. Remove Duplicates from Sorte

作者: 苏州城外无故人 | 来源:发表于2018-12-26 16:55 被阅读0次
题目

思路:设置一个慢指针,一个快指针,同时指向开始数字,快指针遇到的数字与慢指针不同时,慢指针向后移一位,并将快指针的数字赋予慢指针


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

相关文章

网友评论

      本文标题:26. Remove Duplicates from Sorte

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