2014年5月12日星期一

Loop Solution - Remove Duplicates from Sorted Array - Simpler code *

Same idea but simpler code (understand harder:)). Both keeping good ones and removing bad ones will work.

public class Solution {
    public int removeDuplicates(int[] A) {
        int count = 0;
    int len = A.length;
    for (int i = 0; i < len; i++) {
        if (count == 0 || A[i] != A[count - 1]) {
            A[count++] = A[i];
        }
    }
    return count;
    }
}

没有评论: