Simple C++ solution


  • 0
    C
    // Forward declaration of the read4 API.
    int read4(char *buf);
    
    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) {
            int count = 0;
            while (count < n)
            {
                int r = read4(buf);
                count += r;
                buf += r;
                if (r < 4)
                    break;
            }
            int len = min(count, n);
            buf[len] = 0;
            return len;
        }
    };

Log in to reply
 

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