https://leetcode.cn/problems/remove-duplicates-from-sorted-array/

双指针

  1. public static int removeDuplicates(int[] nums) {
  2. int n = nums.length;
  3. if (n == 0)
  4. return 0;
  5. //定义两个指针fast和slow 分别为快指针和慢指针,快指针表示遍历数组到达的下标位置,慢指针表示下一个不同元素要填入的下标位置,
  6. int fast = 1, slow = 1;
  7. while (fast < n) {
  8. if (nums[fast] != nums[fast - 1]) {
  9. nums[slow] = nums[fast];
  10. slow++;
  11. }
  12. fast++;
  13. }
  14. return slow;
  15. }