Saturday, November 21, 2015

[leetcode] Remove Duplicates from Sorted Array II

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Solution {
    public int removeDuplicates(int[] nums) {
        int available = 1;
        int countDup = 1;
        for (int i = 1; i < nums.length; i++){
            if (nums[i] == nums[i-1]){
                if (countDup < 2){
                    nums[available++] = nums[i];
                }
                countDup++;
            }else{
                countDup = 1;
                nums[available++] = nums[i];
            }
        }
        return available;
    }
}

No comments:

Post a Comment