首页 > 代码库 > [leetcode]Read N Characters Given Read4

[leetcode]Read N Characters Given Read4

用read4实现readn...

至调用一次,感觉怎么搞都可以。。。估计这个题有II就是调用多次了。。。

感觉多次勇哥buffer存下多读的那部分就好了。。。

 

// Forward declaration of the read4 API.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) {        char buffer[5];        int cnt = 0;        while (cnt < n) {            int sz = read4(buffer);            memcpy(buf + cnt, buffer, sz);            cnt += sz;            if (sz < 4) break;        }        if (cnt > n) {            buf[n] = \0;            cnt = n;        }        return cnt;    }};

 

[leetcode]Read N Characters Given Read4