Easy Java 1ms Solution


  • 0
    D
    public class Solution {
        public int removeDuplicates(int[] nums) {
            if(nums.length <= 2)
                return nums.length;
            int curIndex = 1;
            int runningIndex = 1;
            int count = 1;
            int curNum = nums[0];
            while(runningIndex < nums.length) {
                if(nums[runningIndex] == curNum) {
                    if(count == 2) {
                        runningIndex++;
                    } else {
                        count++;
                        nums[curIndex++] = curNum;
                        runningIndex++;
                    }
                } else {
                    curNum = nums[runningIndex++];
                    count = 1;
                    nums[curIndex++] = curNum;
                }
            }
            return curIndex;
        }
    }
    

Log in to reply
 

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