Concise c# solution


  • 0
    M
    public class Solution : Reader4 {
        /**
         * @param buf Destination buffer
         * @param n   Maximum number of characters to read
         * @return    The number of characters read
         */
         
        char[] m_buf = new char[4];
        int m_bc;
        int m_bi;
        
        public int Read(char[] buf, int n) {
            int toRead = n;
            int canRead = 0;
            do {
                Array.Copy(m_buf, m_bi, buf, n - toRead, canRead);
                toRead -= canRead;
                m_bi += canRead;
                
                if(m_bi == m_bc) {
                    m_bc = Read4(m_buf);
                    m_bi = 0;
                }
                    
                canRead = Math.Min(toRead, m_bc - m_bi);
            } while(canRead > 0);
            
            return n - toRead;
        }
    }

Log in to reply
 

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