8-line fast C++ solution


  • -2
    W
    class Solution {
    public:
        char mBuf[4];
        int mBufLeft, mBufStart;
        
        Solution() { mBufLeft=0; }
        
        int read(char *buf, int n) {
            int N=n;
            while (n > 0) 
            {
                if (mBufLeft > 0)
                    *(buf++) = mBuf[mBufStart++], mBufLeft--, n--;
                else if ((mBufStart=0, mBufLeft=read4(mBuf)) == 0)
                    break;
            }
            return N-n;
        }
    };

Log in to reply
 

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