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 ...
随机推荐
- 【Java编码准则】の #02不要在client存储未加密的敏感信息
当构建CS模式的应用程序时,在client側存储敏感信息(比如用户私要信息)可能导致非授权的信息泄漏. 对于Web应用程序来说,最常见的泄漏问题是在client使用cookies存放server端获取 ...
- C++ Primer 学习笔记_43_STL实践与分析(17)--再谈迭代器【中】
STL实践与分析 --再谈迭代器[中] 二.iostream迭代[续] 3.ostream_iterator对象和ostream_iterator对象的使用 能够使用ostream_iterator对 ...
- Linux内核分析(二)----内核模块简介|简单内核模块实现
原文:Linux内核分析(二)----内核模块简介|简单内核模块实现 Linux内核分析(二) 昨天我们开始了内核的分析,网上有很多人是用用源码直接分析,这样造成的问题是,大家觉得很枯燥很难理解,从某 ...
- 敏感字符串加密处理(PHP实现)
/** * 敏感字符串加密处理 * @param $raw_str 原始字符串 * @param $before 前面保留的显示位数 * @param $after 后面保留的显示位数 * @para ...
- requestWindowFeature()应用
我们在开发程序是常常会须要软件全屏显示.自己定义标题(使用button等控件)和其它的需求,今天这一讲就是怎样控制Android应用程序的窗口显示. 首先介绍一个重要方法那就是requestWindo ...
- CSM认证培训知识汇总
后两天的CSM训练.我们非常颠覆旧观念,有关的一些观点的简要培训记录: 约定大于监管:监督是管理水平.或旧有格局,该公约是一个团队与自己的法规. 工具:传统的管理工具和服务经理,还有许多人是PM正在使 ...
- 检测ORACLE方法汇总数据块损坏
1:使用初始化参数 使用初始化参数db_block_checksum\db_block_checking能够设置数据库对块的物理一致性和逻辑一致性检查. Db_block_checksum:物理一致性 ...
- 讨厌OpenSSL
在OpenSSL心脏出血后,.我相信很多人都出了血.而流下眼泪...瞬间出现在网上了很多吐槽OpenSSL文章还是那条,窝火一刻仿佛心脏被释放出来,按照这个忙疯了,我吐在嘈杂.在这些年被雪OpenSS ...
- java回顾4 Java基本数据类型
为JAVA基本数据类型.我的实在是有兴趣引用数据类型.在这里,我说的是主应用程序数据类型. 为JAVA荐两个网址: 1.http://blog.sina.com.cn/s/blog_745b874b0 ...
- ASP.NET 5简介
ASP.NET 5简介 解读ASP.NET 5 & MVC6系列(1):ASP.NET 5简介 2015-05-13 09:14 by 汤姆大叔, 3379 阅读, 39 评论, 收藏, 编辑 ...