Share my O(N) time and O(1) space simple solution


  • 0
    W
    public class Solution {
        public int removeDuplicates(int[] nums) {
            final int len;
            int count = 0;
            
            if (nums == null || (len = nums.length) == 0) {
                return count;
            }
            
            for (int n : nums) {
                nums[count++] = n;
                
                if (count < 3) {
                    continue;
                }
                
                if (nums[count - 1] == nums[count - 2] && nums[count - 1] == nums[count - 3]) {
                    --count;
                }
            }
            
            return count;
        }
    }
    

Log in to reply
 

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