My 12ms C solution, easy to understand


  • 0
    L

    Solution: use the first n elements to store the different elements, just as the idea of partition from quick sort.

    int removeDuplicates(int* nums, int numsSize) {
       
     if (nums == NULL || numsSize == 0)
            return 0;
            
        int i, index = 1;
        
        for (i = 1; i < numsSize; i++) {
            if (nums[i - 1] != nums[i]) {
                nums[index++] = nums[i];
            }
        }
        
        return index;
    }

  • 0
    N

    Smart answer. I'm just starting out, so, I wouldn't have thought of this, thanks for the example and a new way to think about how to solve the problem.

    edit:
    Wait!, what about an array like:
    {1, 1, 1, 2, 2, 2, 3, 3, 3, ...} ?? that function would output something like:
    {1, 1, 2, 2, 3, 3, ...} am I correct?


Log in to reply
 

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