[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;
    }
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM