Not as easy as it appears. Share my C++ one.


  • 0
    F
    class Solution {
        char temp[4];
        int pos = 4, end = 4;
    public:
        int read(char *buf, int n) {
            int total = 0;
            for (;pos < end && n > 0; ++pos, ++total, --n, ++buf)
                *buf = temp[pos];
            for(;n >= 4; n-=4, buf+=4)
            {
                int r = read4(buf);
                total += r;
                if(r < 4)
                    return total;
            }
            if (n == 0)
                return total;
            end = read4(temp);
            for (pos = 0; pos < min(end, n); ++pos, ++total, ++buf)
                *buf = temp[pos];
            return total;
        }
    };

Log in to reply
 

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