memmove memset doesn't work on leetcode ?


  • 0
    K

    memset and memmove doesn't work in leetcode ?

    class Solution {
    public:
    void moveByte (int* src, int* dest, int size)
    {
    memmove(src,dest,size);
    /*
    for (int i = 0; i < size; i++)
    {
    *src = *dest;
    src++;
    dest++;
    }
    */
    }

    void setZero(int* src, int size)
    {
        memset(src,0,size);
        /*
        for (int i = 0; i < size; i++)
        {
            src[i] = 0;
        }
        */
    }
    
    void moveZeroes(vector<int>& nums) {
        int prev = -1;
        int size = nums.size();
        int numOfZero = 0;
        for (int i = 0; i < size; i++) 
        {
            if (nums[i] == 0)
            {
                if (prev != -1) 
                { 
                    moveByte(&(nums[prev-numOfZero+1]), &(nums[prev+1]), i-prev-1);
                }
                numOfZero++;
                prev=i;
            }
        }
        if (prev != -1)
        {
            moveByte(&(nums[prev-numOfZero+1]), &(nums[prev+1]), size-prev);
            setZero(&(nums[size-numOfZero]),numOfZero);
        }
        
    }
    

    };


Log in to reply
 

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