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

这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个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的时候用。

参见这里,用全局变量记录之前访问的是否有溢出。

  1. // Forward declaration of the read4 API.
  2. int read4(char *buf);
  3.  
  4. class Solution {
  5. public:
  6. /**
  7. * @param buf Destination buffer
  8. * @param n Maximum number of characters to read
  9. * @return The number of characters read
  10. */
  11. Solution() : buf_len() {
  12.  
  13. }
  14. int read(char *buf, int n) {
  15. char buffer[];
  16. int cnt = ;
  17. if (buf_len > ) {
  18. memcpy(buf, _buf, min(buf_len, n));
  19. cnt += min(buf_len, n);
  20. if (n < buf_len) {
  21. memcpy(_buf, _buf + n, buf_len - n);
  22. buf_len -= n;
  23. } else {
  24. buf_len = ;
  25. }
  26. }
  27. int sz;
  28. while(cnt < n) {
  29. sz = read4(buffer);
  30. memcpy(buf + cnt, buffer, sz);
  31. cnt += sz;
  32. if (sz < ) break;
  33. }
  34. if (cnt > n) {
  35. buf[n] = '\0';
  36. buf_len = cnt - n;
  37. memcpy(_buf, buffer + (sz-buf_len), buf_len);
  38. cnt = n;
  39. }
  40. return cnt;
  41. }
  42. private:
  43. int buf_len;
  44. char _buf[];
  45. };

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. UVALive - 3263 That Nice Euler Circuit (几何)

    UVALive - 3263 That Nice Euler Circuit (几何) ACM 题目地址:  UVALive - 3263 That Nice Euler Circuit 题意:  给 ...

  2. 详细说明C++笔试题,调查超载、盖、多态

    C++可见版本,他说,这本书是采访的主题,调查超载.盖.多态性等概念,比较有代表性的.今天上午,远程辅导 Yan Wang 学生们学习 Qt 时还觉得这个话题,假设你能正确地理解这一主题,注意对于 C ...

  3. Flux是一个Facebook团队的前端开发架构

    Flux是一个Facebook团队的前端开发架构 Flux introduction 本文组成: React 官方文档翻译 相关实践心得. 内容上是Flux的介绍,例子将会在以后写出.一旦稍微多了解一 ...

  4. Json.Net6.0入门学习试水篇

    原文:Json.Net6.0入门学习试水篇 前言 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中 ...

  5. Windows 2008 卸载 IIS7 批处理

    @echo offcolor 0aecho 正在卸载IIS功能,这可能需要几分钟时间...start /w pkgmgr /uu:IIS-WebServerRole;WAS-WindowsActiva ...

  6. MBProgressHUD 问题

    1.MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWi ...

  7. My97DatePicker日历控件日报、每周和每月的选择

    My97DatePicker日历控件日报.每周和每月的选择 1.设计源代码 <%@ page language="java" import="java.util.* ...

  8. Swift学习——Swift解释特定的基础(七)

    Implicitly Unwrapped Optionals    隐式解析选项 如上所述.可选意味着常数或变量"没有值".通过可选if声明来推断是否存在值,假设有值析值. 有时候 ...

  9. 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,就不保证输出的顺序. 那么, ...

  10. Swift——(两)Swift访问元组

    在Swift在,获取元组的值到一个位置,通过三种方法: 1.使用元组变量/常量     @Author: twlkyao转载或者引用请保留此行. let http404Error = (404, &q ...