[leetcode]Remove Duplicates from Sorted Array II
简单题,但也思考了一会。就是pos标志下一个要写入的位置,而current标志下一个可以写入的内容。
public class Solution { public int removeDuplicates(int[] A) { if (A.length == 0 || A.length == 1) return A.length; int pos = 0; int current = 0; boolean encounted = false; while (current < A.length) { if (current == 0 || A[current] != A[current-1]) { A[pos] = A[current]; pos++; current++; encounted = false; } else // A[current] == A[current-1] { if (!encounted) { A[pos] = A[current]; pos++; current++; encounted = true; } else { current++; } } } return pos; } }