C# O(n) and O(1) space


  • 0
    Y
    public class Solution {
        public int RemoveDuplicates(int[] nums) {
            if(nums.Length<3)
                return nums.Length;
            //clear logic will be the runner run and count, start just add
            int runner=2,cur=2;
            while(runner<nums.Length)
            {
                if(nums[runner]!=nums[cur-2])
                {
                    nums[cur]=nums[runner];
                    cur++;
                }
                runner++;
            }
            return cur;
        }
    }

Log in to reply
 

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