[刷题]Remove Duplicates from Sorted Array II

来源:互联网 发布:grub4dos引导linux 编辑:程序博客网 时间:2024/05/13 03:18

[LintCode]Remove Duplicates from Sorted Array II

public class Solution {    /**     * @param A: a array of integers     * @return : return an integer     */    public int removeDuplicates(int[] nums) {        // 2015-4-12 待改进        if (nums == null || nums.length == 0) {            return 0;        }        if (nums.length == 1) {            return 1;        }        int index = 0;        boolean secondSame = true; //关键:与第一题的区别        for (int i = 1; i < nums.length; i++) {            if (nums[index] != nums[i]) {                secondSame = true;                index++;                nums[index] = nums[i];            } else if (secondSame) {                secondSame = false;                index++;                nums[index] = nums[i];            }        }        return index + 1;    }}


0 0
原创粉丝点击