C++ clean 5 lines


  • 0

    5 lines:

    int read(char *buf, int n) {
            int cnt = 0;
            for (int k = 4; n > 0 && k == 4; n -= k) { 
                cnt += min(k = read4(buf + cnt), n); 
            }
            return cnt;
    }
    

    To make it even cleaner, we put only necessary chars in buf:

    int read(char *buf, int n) {
            int cnt = 0, k = 4;
            
            while (n > 0 && k == 4) {
                char *temp;
                k = read4(temp);
                for (int i = 0; n && i < k; i++, n--) {
                    buf[cnt++] = temp[i];
                }
            }
            
            return cnt;
    }
    

Log in to reply
 

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