My AC Solution(C)


  • 0
    C

    void swap(int *x, int *y)
    {
    int temp = *x;
    *x = *y;
    y = temp;
    }
    void moveZeroes(int
    nums, int numsSize) {
    if (nums == NULL || numsSize <= 0)
    return;
    int current = 0;
    int indexToZero = 0;
    while (current < numsSize)
    {
    if (nums[current] != 0)
    {
    swap(&nums[current], &nums[indexToZero]);
    indexToZero++;
    }
    current++;

    }
    

    }


  • 0
    F

    Pleast format your code!

    Did you copy paste your solution? Since your code has syntax errors I'm a little bit confused!
    Except that yes your code works.

    class Solution {
    public:
        void swap(int *x, int *y) { 
            int temp = *x; *x = *y; *y = temp; 
        } 
        
        void moveZeroes(int nums, int numsSize) { 
            if (nums == NULL || numsSize <= 0) 
                return; 
            int current = 0; 
            int indexToZero = 0; 
            while (current < numsSize) { 
                if (nums[current] != 0) { 
                    swap(&nums[current], &nums[indexToZero]); indexToZero++; 
                    
                } 
            current++;
            }
        }
    };

Log in to reply
 

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