首页 > 代码库 > Leetcode#80 Remove Duplicates from Sorted Array II

Leetcode#80 Remove Duplicates from Sorted Array II

原题地址

 

简单模拟题。

从先向后遍历,如果重复出现2次以上,就不移动,否则移动到前面去

 

代码:

 1 int removeDuplicates(int A[], int n) { 2         if (n == 0) return n; 3          4         int len = 1; 5         int dupSum = 1; 6          7         for (int i = 1; i < n; i++) { 8             if (A[i] == A[i - 1]) { 9                 dupSum++;10             }11             else12                 dupSum = 1;13             if (dupSum <= 2) {14                 A[len] = A[i];15                 len++;16             }17         }18         19         return len;20 }

 

Leetcode#80 Remove Duplicates from Sorted Array II