Simple Java Solution using two pointers


  • 0
    D
    public class Solution {
        public int removeDuplicates(int[] nums) {
            int n=nums.length;
            if(n<=2) return n;
            int i=1,j=1,count=1;
            while(j<n){
                if(nums[j]!=nums[j-1]){
                    nums[i++]=nums[j];
                    count=1;
                }else{
                    if(count<2){
                        nums[i++]=nums[j];
                        count++;
                    }
                }
                j++;
            }
            return i;
        }
    }

  • 0

    in else part, what should be done if count >= 2?


Log in to reply
 

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