Sharing my 0ms C++ solution


  • 0
    T
    // Forward declaration of the read4 API.
    int read4(char *buf);
    
    class Solution {
    private:
        char bufRead[5];
        int  nLeft;
        int  bufStart;
        
    public:
        /**
         * @param buf Destination buffer
         * @param n   Maximum number of characters to read
         * @return    The number of characters read
         */
         
         Solution()
         {
             nLeft=0;
         }
         
         ~Solution(){}
         
        int read(char *buf, int n) {
            char* p = buf;
            int nToRead = n;
            while(nToRead>0)
            {
                if(nLeft>0)
                {
                    *p = bufRead[bufStart];
                    p++;
                    bufStart++;
                    nLeft--;
                    nToRead--;
                }
                else
                {
                    nLeft = read4(bufRead);
                    if(nLeft==0)
                        break;
                    else
                    {
                        bufStart = 0;
                    }
                }
            }
            *p = 0;
            return (n-nToRead);
        }
    };

Log in to reply
 

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