leetcode[158] Read N Characters Given Read4 II - Call multiple times
想了好一会才看懂题目意思,应该是:
这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个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的更多相关文章
- ✡ 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 ...
- [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 ...
- 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- 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 ...
- [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 ...
- [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 ...
- LeetCode Read N Characters Given Read4 II - Call multiple times
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...
- 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 ...
- [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 ...
随机推荐
- 使用log4j日志-配置载入问题
1.在eclipse中,把log4j.properties放在类路径下,在项目启动时就会自己主动载入. 2.在idea中.把log4j.properties放在类路径下,可是项目启动时不能直接载入(原 ...
- NSIS:使用nsWindows.nsh头文件调整窗体大小
原文 NSIS:使用nsWindows.nsh头文件调整窗体大小 此方法只能简单实现调整窗体大小,但不完美,调整后窗体上其他的控件都需要调整大小或位置,比较麻烦,轻狂不建议使用呵! ;加头文件!inc ...
- Java回合阵列List
package com.mine.practice.arrtolist; import java.util.ArrayList; import java.util.Arrays; import jav ...
- java.lang.Runnable接口
大家都知道使用线程的2种方式,一是继承Thread类,二是实现Runnable接口.实际上,即使你实现了Runnable接口,终于还是要构造一个Thread类的对象.看过Thread源码发现,事实上这 ...
- win8 64位使用plsql developer连接oracle数据库问题
问题:win8的64使用位系统plsql developer本地连接oracle莫名其妙的问题发生在数据库时.错误消息框,甚至可能是空的. 原因:它表示,互联网,的原因,预计在64位系统安装在64位O ...
- HDOJ 3037 Saving Beans
如果您有n+1树,文章n+1埋不足一棵树m种子,法国隔C[n+m][m] 大量的组合,以取mod使用Lucas定理: Lucas(n,m,p) = C[n%p][m%p] × Lucas(n/p,m/ ...
- cocos2d-x 3.0rc1 创建project
1.进入bin文件夹 2.打开CMD命令行窗口中输入命令,然后按Enter(-p 包名 -l 语言 -d 新project存储路径)
- 豆瓣api之OAuth认证
豆瓣api通过OAuth允许第三方应用访问用户数据,所以OAuth认证就是我们整个project的基础了. OAuth认证听起来挺神秘,其实挺简单的. 现在的大型网站的开放平台的认证几乎都是采用OAu ...
- 堆VS栈
c#堆VS栈(Part One) 前言 本文主要是讲解C#语言在内存中堆.栈的使用情况,使读者能更好的理解值类型.引用类型以及线程栈.托管堆. 首先感谢原文作者:Matthew Cochran 为我们 ...
- vs2012 + web api + OData + EF + MYsql
vs2012 + web api + OData + EF + MYsql 开发及部署 先说下我的情况,b/s开发这块已经很久没有搞了,什么web api .MVC.OData都只是听过,没有实际开发 ...