Java in-place sol, O(n) time complexity


  • 0
    J
    public class Solution {
        public int removeDuplicates(int[] nums) {
            int cur = 1;
            int twiceco = 0;
            for(int i=1; i<nums.length; i++){
                if(nums[i] != nums[cur-1]){
                    twiceco = 0; //count the appearance of integers.
                    nums[cur] = nums[i];
                    cur++;
                }
                else{
                    if(twiceco == 1){
                       continue; 
                    }
                    else{
                        nums[cur] = nums[i];
                        cur++;
                        twiceco++;
                    }
                }
            }
            return cur;
        }
    }

Log in to reply
 

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