Neat and Simple C++ Solution


  • 0
    C
    int read4(char *buf);
    
    class Solution {
    public:
        /**
         * @param buf Destination buffer
         * @param n   Maximum number of characters to read
         * @return    The number of characters read
         */
        int read(char *buf, int n) {
            int size=n, r;
            while ((r=read4(buf)) > 0 && size>=4) {
                buf += r;
                size = size-r >= 0 ? size-r : 0;
            }
            if (size <= r)
                size = 0;
            else
                size -= r;
            return n-size;
        }
    };

  • 0
    G

    I'm wondering why should we considering the circumstance that " size-r<0 " while size>=4 and r<=4 here. Will there be any difference if just write " size-=r " ? ty.


Log in to reply
 

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