2ms Java solution


  • 0
    F
        public int removeDuplicates(int[] nums) {
            int i = 0, k = 0;
            while (i < nums.length) {
                int same = 1, s = i;
                while (i < nums.length - 1 && nums[i] == nums[i + 1]) {
                    same++;
                    ++i;
                }
                if (same > 2) {
                    same = 2;
                } 
                for (int j = 0; j < same; j++) {
                    nums[k++] = nums[s + j];
                }
                ++i;
            }
            return k;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.