LC.26.Remove Duplicates from Sorted Array

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

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

 1 public int removeDuplicates(int[] nums) {
 2         if (nums == null || nums.length <=1){
 3             return nums.length ;
 4         }
 5         /*because its sorted
 6         * return [0, slow)
 7         * */
 8         //slow pointer: 启示的位置 从1 开始
 9         int slow = 1 ;
10         //fast pointer
11         /*
12                          s
13                     0,   0,   1,    1,   1,    2,    2,    3,   3,   4
14                          i
15         * */
16         for (int i = 1; i < nums.length; i++) {
17             if (nums[slow-1] != nums[i]){
18                 nums[slow++] = nums[i];
19             }
20         }
21         return slow;
22     }

需要和 LC.80.Remove Duplicates from Sorted Array II    对比参考

原文地址:https://www.cnblogs.com/davidnyc/p/8859327.html