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 ...
 
随机推荐
- 章节2:SQL之多表连接
			
原文:章节2:SQL之多表连接 Sql的多表连接关系有:内连接.外连接和交叉连接. 先建立两个用于演示的表: TB_Characters: Id Character 1 内向 2 外向 3 中性性格 ...
 - log4j与commons-logging,slf4j的关系(转)
			
前面有一篇日志中简单的介绍了 log4j,同时也介绍了它与commons-logging的关系,但是突然冒出来一个slf4j,并且slf4j有取代commons-logging的趋势,所以,我们可以推 ...
 - PHP MVC自己主动RBAC自己主动生成的访问路由
			
使用的关键点: ReflectionClass class Rbac extends MY_Controller { public function index() { $arr = glob( __ ...
 - C# 线程知识--使用Task执行异步操作
			
在C#4.0之前需要执行一个复杂的异步操作时,只能使用CLR线程池技术来执行一个任务.线程池执行异步任务时,不知道任务何时完成,以及任务的在任务完成后不能获取到返回值.但是在C#4.0中引人了一个的任 ...
 - hdu 1700 Points on Cycle 水几何
			
已知圆心(0,0)圆周上的一点,求圆周上另外两点使得三点构成等边三角形. 懒得推公式,直接用模板2圆(r1=dist,r2=sqrt(3)*dist)相交水过 #include<cstdio&g ...
 - jquery选择器基础知识
			
$("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("di ...
 - jquery动态刷新局部表单
			
想实现一个效果就是选择某个年份:然后再action中按该年份查找数据库中的数据,返回到页面表单中显示. 1.添加登记年度的changge事件,也是异步请求. $(document).ready(fun ...
 - jsmart 前结合案例
			
前绑定jsmart这是一个不错的选择.之前通过经常使用的项目中的.最近涉及的领域的后端部.jsmart有些使用相对较少,主要是因为他想引用文件,我写的模板,在一个简单的项目,直接使用js界,很复杂的前 ...
 - oracle创建user具体指示
			
一个.用户的概念 用户,这是user,通俗的讲就是参观oracle数据库"人".在oracle在.的各种安全参数的用户可控制,为了保持数据库的安全性,的概念包括模型(schema) ...
 - CSS3+HTML5特效9 - 简单的时钟
			
原文:CSS3+HTML5特效9 - 简单的时钟 效果演示(加快了100倍) 实现原理 利用CSS3的transform-origin 及 transform 完成以上效果. 代码及说 ...