6 lines C++


  • 0
        int read(char *buf, int n) {
            int sum = 0;
            for(int i = 0; i < n; i += 4){
                sum += read4(buf);
                buf += 4;
            }
            return min(sum, n);
        }
    

    Or

    int read(char *buf, int n) {
            int sum = 0, i = 4;
            while(sum < n && i == 4){
                i = read4(buf);
                buf += 4;
                sum += i;
            }
            return min(sum, n);
        }
    

Log in to reply
 

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