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

这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个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. 全栈JavaScript路(八)得知 CDATASection 种类 节点

    CDATASection 只船舶类型节点 基于XML 文件.演出CDATA 数据. 构造函数: CDATASection function(){[native code]} CDATASection ...

  2. 一个简单的Java死锁示例(转)

    在实际编程中,要尽量避免出现死锁的情况,但是让你故意写一个死锁的程序时似乎也不太简单(有公司会出这样的面试题),以下是一个简单的死锁例子,程序说明都写着类的注释里了,有点罗嗦,但是应该也还是表述清楚了 ...

  3. VB.NET之错误异常处理

    相对于VB而言,VB.NET中引入了很多特色.当中最吸引我的就是引入了结构化异常处理. 尽管VB.NET仍然支持OnError Goto类型的异常处理,可是这样做并非非常好.相比而言,结构化异常处理更 ...

  4. 开源TinyXML 最简单的新手教程

    TinyXML它是基于一个非常受欢迎的现在DOM型号XML解析器,简单易用且小巧玲珑,很适合存储简单数据.配置文件. 该项目属于开源项目,在sourceforge上边的链接是:http://sourc ...

  5. ScrollView 在嵌套 ViewPager 时出现的问题

    1.在ViewPager 外面嵌套ScrollView 时导致ViewPager 中内容不显示,解决的办法是在ScrollView 标签下增加 android:fillViewport="t ...

  6. hdu Text Reverse

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 单词翻转! 代码: #include <stdio.h> #include < ...

  7. 经典算法题每日演练——第十六题 Kruskal算法

    原文:经典算法题每日演练--第十六题 Kruskal算法 这篇我们看看第二种生成树的Kruskal算法,这个算法的魅力在于我们可以打一下算法和数据结构的组合拳,很有意思的. 一:思想 若存在M={0, ...

  8. Linux makefile 课程 非常具体的,和理解

    最近的一项研究Linux根据C计划,我买了一个电话<Linux环境C编程指南>阅读makefile这使他看起来困惑,我可能无法理解. 于是google到了下面这篇文章. 通俗易懂. 然后把 ...

  9. linux_根据关键词_路径下递归查找code

    1:进入想查找的项目根目录 2:根据关键词查找 find . -name "*" |xargs grep -F '10.26'

  10. 计算4000000000内的最大f(n)=n值---字符串的问题python实现(五岁以下儿童)

    问题: 写一个函数,计算4 000 000 000 以内的最大的那个f(n)=n的值,函数f的功能是统计全部0到n之间全部含有数字1的数字和.比方:f(13)= 6,由于"1"在& ...