想了好一会才看懂题目意思,应该是:

这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作。上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n,并且将相应的字符存在buf里。现在调用多次的话就可能存在以下的例子:

例如文件case是:1,2,3,4,5,6,7

如果要实现read5,先用read4读四个到buf,再用read4读剩下的3个到buf+4之后,但是read5一次最多读5个到buf,所以read4多读的2个就要存起来,防止下次调用read5的时候用。

参见这里,用全局变量记录之前访问的是否有溢出。

// 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
*/
Solution() : buf_len() { }
int read(char *buf, int n) {
char buffer[];
int cnt = ;
if (buf_len > ) {
memcpy(buf, _buf, min(buf_len, n));
cnt += min(buf_len, n);
if (n < buf_len) {
memcpy(_buf, _buf + n, buf_len - n);
buf_len -= n;
} else {
buf_len = ;
}
}
int sz;
while(cnt < n) {
sz = read4(buffer);
memcpy(buf + cnt, buffer, sz);
cnt += sz;
if (sz < ) break;
}
if (cnt > n) {
buf[n] = '\0';
buf_len = cnt - n;
memcpy(_buf, buffer + (sz-buf_len), buf_len);
cnt = n;
}
return cnt;
}
private:
int buf_len;
char _buf[];
};

leetcode[158] Read N Characters Given Read4 II - Call multiple times的更多相关文章

  1. ✡ leetcode 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) --------- java

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  2. [leetcode]158. Read N Characters Given Read4 II - Call multiple times 用Read4读取N个字符2 - 调用多次

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  3. 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...

  4. 158. Read N Characters Given Read4 II - Call multiple times

    题目: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the ...

  5. [Locked] Read N Characters Given Read4 & Read N Characters Given Read4 II - Call multiple times

    Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...

  6. [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  7. LeetCode Read N Characters Given Read4 II - Call multiple times

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...

  8. Read N Characters Given Read4 II - Call multiple times

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  9. [LeetCode] 157. Read N Characters Given Read4 用Read4来读取N个字符

    The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...

随机推荐

  1. C#值传递和按引用传递

    知识点: 值类型和引用类型  为值类型,,据        对于引用类型来说,栈中存储的是堆中对象的地址 值传递和引用传递 对于值传递,传递的是栈中保存的数据        对于引用传递.传递的是栈本 ...

  2. Android Studio常见报错及处理办法

    在Android Studio上点了update,系统自动升级,自动重启Android Studio后,以前的项目Gradle正常编译: Unable to start the daemon proc ...

  3. ssh配置连接

    SSH构造: 1.改动vi /etc/ssh/sshd_config,依据模板将要改动的參数凝视去掉并改动參数值: Port 22 指定SSH连接的port号,安全方面不建议使用默认22port Pr ...

  4. git checkout 命令详解(转)

    在日常的git操作中,git checkout——检出,是我们的常用命令.最为常用的两种情形是创建分支和切换分支. 在下面的命令中,使用了一些简写,在这里说明一下: git st # git stat ...

  5. Angular规范

     只记录一些自己未曾用过,但觉得对以后的项目有帮助的规范 一 Javascript闭包 把Angular组件包装到一个立即调用函数表达式中(IIFE). 为什么?:把变量从全局作用域中删除了,这有助于 ...

  6. 【剑指offer】堆栈推弹出序列

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26077863 剑指offer上的第22题,九度OJ上AC. 题目描写叙述: 输入两个整数序 ...

  7. ORA-07445: :一个意料之外的问题发生了 核心转储 [ldxsnf()+625] [SIGSEGV

    ALERT登录错误消息: Mon Jan 20 15:03:22 2014 Incremental checkpoint up to RBA [0x442f.abd.0], current log t ...

  8. WebApi的一种集成测试写法(in-memory)

    WebApi的一种集成测试写法(in-memory)   大家是如何对webApi写测试的呢? 1.利用Fiddler直接做请求,观察response的内容. 2.利用Httpclient做请求,断言 ...

  9. 三个水杯 (bfs)

    给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算.现在要求你写出一个程序,使其输出使初始状态到达目标状态的 ...

  10. CSS浏览器兼容性问题集()两

    11.非常适合    高度适合于被改变时所述内目标高度的外层的高度不能自己主动调节,尤其是排队对象时margin 要么paddign 时. 例:   #box {background-color:#e ...