Easy C++ solution w/ memcpy


  • 0
    S


    class Solution {
    public:
    /**
    * @param buf Destination buffer
    * @param n Maximum number of characters to read
    * @return The number of characters read
    */

    int read(char *buf, int n) {
        char temp[4];
        int bytesRead = 0;
        int totalBytesRead = 0;
        while(n > 0 && (bytesRead = read4(temp)) > 0)
        {
            memcpy(buf + totalBytesRead, temp, min(n, bytesRead));
            totalBytesRead += min(n, bytesRead);
            n -= bytesRead;
        }
        return totalBytesRead;
     }
    

    };


Log in to reply
 

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