My simple java solution which beats 66%


  • 1
    X
    public class Solution {
    public int removeDuplicates(int[] nums) {
        int l = nums.length;
        if (l <= 2) return l;
        int i = 2; //new length
        int j = 2; //raversal
        while (j < l) {
            if (nums[j] == nums[i - 2]) {
                j++;
            } else {
                nums[i] = nums[j];
                i++;
                j++;
            }
        }
        return i;
    }
    

    }


  • 0
    D

    I think it is inadvisable to use "l" as the variable name, because it looks like "1".


  • 0
    D

    And at first, I think I will check whether the array is null of has a length equal to 1.


Log in to reply
 

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