题目:

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.

链接:  http://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/

题解:

又是比较难理解题意的一题...call multiple times,举个例子呀, 没例子咋理解。尝试了好几次才弄明白意思。给定read4,跟上一题一样,求可以call multiple times的read()。假如文件字符串是"abc",我们调用read(1),应该返回"a",再调用read(2),应该返回bc"。这里要注意的是,之前我们再第一次调用的时候,read4就已经读取了"abc",所以这道题其实就是要在上一题的基础上处理这种情况。解决方法不难,我们可以用一个queue来存储多读取的部分,然后在下次调用read的时候,根据情况判断,先读queue里面上次读剩下的数据,再进行下面的读取。也可以把read4的buffer放在global,然后用一个int型的offset来记录上次读了read4的多少。由于这个global buffer不会超过4,所以space complexity算是O(1)的。 LeetCode有些题真的很难读懂题意。

Time Complexity - O(n), Space Complexity - O(1)。

  1. /* The read4 API is defined in the parent class Reader4.
  2. int read4(char[] buf); */
  3.  
  4. public class Solution extends Reader4 {
  5. /**
  6. * @param buf Destination buffer
  7. * @param n Maximum number of characters to read
  8. * @return The number of characters read
  9. */
  10.  
  11. private Queue<Character> q;
  12. private boolean EOF;
  13.  
  14. public Solution() {
  15. this.q = new LinkedList<>();
  16. this.EOF = false;
  17. }
  18.  
  19. public int read(char[] buf, int n) {
  20. char[] read4Buffer = new char[4];
  21. int bytesRead = 0;
  22.  
  23. while (!q.isEmpty() && bytesRead < n) //try read queue buffer first
  24. buf[bytesRead++] = q.poll();
  25.  
  26. while(!this.EOF && bytesRead < n) {
  27. int read4Bytes = read4(read4Buffer);
  28. if(read4Bytes < 4)
  29. this.EOF = true;
  30. int bytes = Math.min(n - bytesRead, read4Bytes);
  31. System.arraycopy(read4Buffer, 0, buf, bytesRead, bytes);
  32. bytesRead += bytes;
  33.  
  34. if(bytes < 4) { //push read4 reminder to q for next read
  35. for(int i = bytes; i < read4Bytes; i++)
  36. this.q.offer(read4Buffer[i]);
  37. }
  38. }
  39.  
  40. return bytesRead;
  41. }
  42. }

二刷:

这回题意理解得还算比较顺利。就是给一个read4()的api,每次最多读取4个char,要求实现read,读取n个char。这里n可以小于read4()返回的数字,也可以大于。

问题的关键是要储存多读的字符,我们使用一个queue就可以简单解决。每次调用read()的时候,先检查queue是否为空,假如不为空则先从queue中读取。接下来,假如仍然没有读到n个字符,我们就跟上一题一样,调用read4()来不断读取。 要注意多读的字符,我们要保存到queue中。最后返回一共读取的字符数就可以了。

也可以理解为把缓存中的东西持久化。

Java:

Time Complexity - O(n), Space Complexity - O(1)。

  1. /* The read4 API is defined in the parent class Reader4.
  2. int read4(char[] buf); */
  3.  
  4. public class Solution extends Reader4 {
  5. /**
  6. * @param buf Destination buffer
  7. * @param n Maximum number of characters to read
  8. * @return The number of characters read
  9. */
  10. Queue<Character> remainingChars = new LinkedList<>();
  11.  
  12. public int read(char[] buf, int n) {
  13. char[] read4Buf = new char[4];
  14. int read4Count = 0;
  15. int totalCharsRead = 0;
  16. while (remainingChars.size() > 0 && totalCharsRead < n) {
  17. buf[totalCharsRead++] = remainingChars.poll();
  18. }
  19. //if (totalCharsRead == n) return n;
  20. while ((read4Count = read4(read4Buf)) > 0) {
  21. int i = 0;
  22. while (i < read4Count && totalCharsRead < n) {
  23. buf[totalCharsRead++] = read4Buf[i++];
  24. }
  25. while (i < read4Count) {
  26. remainingChars.offer(read4Buf[i++]);
  27. }
  28. }
  29. return totalCharsRead;
  30. }
  31. }

Reference:

https://leetcode.com/discuss/19581/clean-accepted-java-solution

https://leetcode.com/discuss/21219/a-simple-java-code

https://leetcode.com/discuss/21393/finally-get-question-understood-and-ac-by-c

https://leetcode.com/discuss/25200/my-python-40ms-solution

http://www.cnblogs.com/EdwardLiu/p/4240616.html

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

  1. 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...

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

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

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

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

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

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

随机推荐

  1. Android之 Fragment

    什么是Fragment: Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局, ...

  2. 测试中的几个case

    一.页面上对引起 大量数据提交的 按钮/链接 点击一次后,  disable 需求: 对于重要的表单.数量庞大/响应慢的系统,在做提交时, 又有页面还在loading状态, 此时连续做两次点击, 经常 ...

  3. Objective-C 学习笔记(Day 3,上)

    ------------------------------------------- 类方法   ①类方法:        + 开头的方法(定义的过程形式和对象方法一样,只不过 + 开头,这是唯一的 ...

  4. Objective-C 学习笔记(Day 1)

    -------------------------------------------- Hello World //引入头文件 //c中的引入头文件的方式 //#include <stdio. ...

  5. .net远程连接oracle数据库不用安装oracle客户端

    asp.net远程连接oracle数据库不用安装oracle客户端的方法下面是asp.net连接远程Oracle数据库服务器步骤: 1.asp.net连接oracle服务器需要添加Sytem.Data ...

  6. 05顺序队列_Queue--(栈与队列)

    #include "stdio.h" #include "stdlib.h" #include "io.h" #include " ...

  7. Update files embedded inside CAB file.

    References: https://community.flexerasoftware.com/showthread.php?182791-Replace-a-single-file-embedd ...

  8. linksys wrt160nv3 刷dd-wrt固件

    家中有个闲置的wrt160nv3路由器,无意中在网上发现可以刷dd-wrt固件来实现更多功能.目前家里电信光猫F460的自带无线使用起来不是很稳定,就想把wrt160nv3刷成dd-wrt来当做一个A ...

  9. sprintf函数php的详细使用方法

    PHP sprintf() 函数 先说下为什么要写这个函数的前言,这个是我在微信二次开发的一个token验证文档也就是示例文档看到的一个函数,当时非常不理解,于是查了百度,但是很多结果都很笼统,结果也 ...

  10. YII2框架动态创建表模型

    YII2框架动态创建表模型 在YII2中,每个表对应一个model类 在开发过程中,我们在填写一个大型表单的时候,表单里有N个select下拉列表,每个下拉select来自于不同的表: 如果要在程序里 ...