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 ...
随机推荐
- UVALive - 3263 That Nice Euler Circuit (几何)
UVALive - 3263 That Nice Euler Circuit (几何) ACM 题目地址: UVALive - 3263 That Nice Euler Circuit 题意: 给 ...
- 详细说明C++笔试题,调查超载、盖、多态
C++可见版本,他说,这本书是采访的主题,调查超载.盖.多态性等概念,比较有代表性的.今天上午,远程辅导 Yan Wang 学生们学习 Qt 时还觉得这个话题,假设你能正确地理解这一主题,注意对于 C ...
- Flux是一个Facebook团队的前端开发架构
Flux是一个Facebook团队的前端开发架构 Flux introduction 本文组成: React 官方文档翻译 相关实践心得. 内容上是Flux的介绍,例子将会在以后写出.一旦稍微多了解一 ...
- Json.Net6.0入门学习试水篇
原文:Json.Net6.0入门学习试水篇 前言 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中 ...
- Windows 2008 卸载 IIS7 批处理
@echo offcolor 0aecho 正在卸载IIS功能,这可能需要几分钟时间...start /w pkgmgr /uu:IIS-WebServerRole;WAS-WindowsActiva ...
- MBProgressHUD 问题
1.MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWi ...
- My97DatePicker日历控件日报、每周和每月的选择
My97DatePicker日历控件日报.每周和每月的选择 1.设计源代码 <%@ page language="java" import="java.util.* ...
- Swift学习——Swift解释特定的基础(七)
Implicitly Unwrapped Optionals 隐式解析选项 如上所述.可选意味着常数或变量"没有值".通过可选if声明来推断是否存在值,假设有值析值. 有时候 ...
- In Oracle 11g, how to change the order of the results of a sql without “order by”?(转)
oracle 11g 当sql语句中不加order by的时候,好像是按rowid的顺序返回结果的.我也看过一些相关的文档,oracle的官方意思就是不加order by,就不保证输出的顺序. 那么, ...
- Swift——(两)Swift访问元组
在Swift在,获取元组的值到一个位置,通过三种方法: 1.使用元组变量/常量 @Author: twlkyao转载或者引用请保留此行. let http404Error = (404, &q ...