[LeetCode] Remove Duplicates from Sorted Array II

来源:互联网 发布:铝合金门 知乎 编辑:程序博客网 时间:2024/05/16 12:10
int removeDuplicates2(int A[], int n) {if (n < 3){return n;}int index = 1, length = n, times = 1,lastnum = A[0];for (int i = 1; i < n; i++){if (A[i] == lastnum){times++;if (times <= 2){A[index] = A[i];index++;}else{length--;}}else{A[index] = A[i];index++;lastnum = A[i];times = 1;}}return length;}

0 0
原创粉丝点击