C++ solution, short with explanations


  • 0
    G
    class Solution {
    public:
        /**
         * @param buf Destination buffer
         * @param n   Maximum number of characters to read
         * @return    The number of characters read
         */
         
        vector<char> Temp;
        int start;
        int NumOfElements;
        
        Solution()
        :
        start(0),
        NumOfElements(0),
        Temp(4,'0')
        {
        }
         
        int read(char *buf, int n) 
        {
            int i = 0;
            
            while(i < n)
            {
                //
                // if start < NumOfElements
                // first fill the buffer from Temp
                //
                
                if(start < NumOfElements)
                {
                    buf[i++] = Temp[start++];
                }
                else
                {
                    //
                    //  Read NumOfElements chars
                    //  into Temp. If there are no more
                    //  chars to read break;
                    //
                    
                    start = 0;
                    NumOfElements = read4(&Temp[0]);
                    
                    if(NumOfElements == 0)
                    {
                        break;
                    }
                }
            }
            
            return (i);
        }
    };

Log in to reply
 

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