原题链接在这里:https://leetcode.com/problems/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 actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.

By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.

Note:
The read function may be called multiple times.

题解:

需要多次调用,用queue来保存前一次调用read4没用完的数据.

read时先用queue中的数据添加到buf中,若是不够再call read4.

在读够n个char后若是read4Buff中还有可用数据,加到queue中.

Note: declear rest first, but not use i < n - readSum in the while condidtion since readSum is changing.

Time Complexity: read, O(n).

Space: O(1). queue的大小不会超过4.

AC Java:

  1. /**
  2. * The read4 API is defined in the parent class Reader4.
  3. * int read4(char[] buf);
  4. */
  5. public class Solution extends Reader4 {
  6. LinkedList<Character> que = new LinkedList<>();
  7.  
  8. /**
  9. * @param buf Destination buffer
  10. * @param n Number of characters to read
  11. * @return The number of actual characters read
  12. */
  13. public int read(char[] buf, int n) {
  14. int readSum = 0;
  15. // 先用queue中剩余的上次结果加到buf中
  16. while(readSum < n && !que.isEmpty()){
  17. buf[readSum++] = que.poll();
  18. }
  19.  
  20. // 若是不够再调用read4 API
  21. boolean eof = false;
  22. char [] temp = new char[4];
  23. while(!eof && readSum < n){
  24. int count = read4(temp);
  25. eof = count < 4;
  26. int rest = n-readSum;
  27.  
  28. int i = 0;
  29. while(i < count && i < rest){
  30. buf[readSum++] = temp[i++];
  31. }
  32.  
  33. // 把当前read4Buff中没有读的有用char加到queue中
  34. if(i == rest){
  35. while(i < count){
  36. que.add(temp[i++]);
  37. }
  38. }
  39. }
  40.  
  41. return readSum;
  42. }
  43. }

类似Read N Characters Given Read4.

LeetCode Read N Characters Given Read4 II - Call multiple times的更多相关文章

  1. [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 ...

  2. [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 ...

  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. ✡ 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 ...

  5. leetcode[158] Read N Characters Given Read4 II - Call multiple times

    想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...

  6. [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 ...

  7. 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 ...

  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] Read N Characters Given Read4 I & II

    Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...

随机推荐

  1. linux建立ssh信任关系

    一.建立SSH信任将A主机做为客户端(发起SSH请求 ip:192.168.200.170)将B主机作为服务器端(接收ssh请求   ip:192.168.200.149)以上以主动发起SSH登录请求 ...

  2. 【BZOJ】1100: [POI2007]对称轴osi

    题意 给一个\(n(1 \le n \le 100000)\)个点不自交的多边形,求对称轴数目. 分析 将多边形表示成长度和角的形式(用有向面积来表示角也行),然后匹配. 题解 匹配可以用kmp或ma ...

  3. 【CodeVS】 p1225 八数码难题

    题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.问题描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字 ...

  4. linux 下如何打开core dump文件开关

    dump文件可以在程序crash时,方便我们查看程序crash的地方和上下文信息.在window下,要能生成dump文件,需要自己编写相应的代码.不过现在网上可以找到相应的代码,只要把它下载后然后加到 ...

  5. AngularJS 乱记

    1. 前端简单逻辑 <title data-ng-bind="{true:' ('+notice_count+') '}[notice_count > 0]+{true:glob ...

  6. Windows Phone Foreground Toast

    Basically ToastPrompt is an UI component that derives from the Coding4Fun toolkit's abstract PopUp&l ...

  7. 用atom写LaTeX文档

    下载并安装Tex Live: 下载页面 下载并安装atom:下载页面 打开atom File -> Settings -> Install 搜索并安装: language-latex la ...

  8. TLV简介

    引子: 前段时间在项目中第一次接触TLV,项目中用这种格式来传输图片,语音等. 关于TLV TLV是一种可变的格式,意为:Type类型, Lenght长度,Value值.Type:该字段是关于标签和编 ...

  9. [LintCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  10. 单页Web应用:

    概念: Web应用程序: WEB应用程序一般是B(浏览器)/S(服务器)模式.Web应用程序首先是“应用程序”,和用标准的程序语言,如C.C++等编写出来的程序没有什么本质上的不同.然而Web应用程序 ...