C++ solution


  • 0
    N
    // 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, pos = 0;
            char tmp[4];
            while (pos < n) {
                count = read4(tmp);
                count = min(count, n - pos);
                for (int i = 0; i < count; i++) {
                    buf[pos++] = tmp[i];
                }
                //  n % 4 != 0
                if(count < 4) break;
            }
            
            return pos;
         }
    };
    

Log in to reply
 

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