题目:

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)。

/* The read4 API is defined in the parent class Reader4.
int read4(char[] buf); */ public class Solution extends Reader4 {
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/ private Queue<Character> q;
private boolean EOF; public Solution() {
this.q = new LinkedList<>();
this.EOF = false;
} public int read(char[] buf, int n) {
char[] read4Buffer = new char[4];
int bytesRead = 0; while (!q.isEmpty() && bytesRead < n) //try read queue buffer first
buf[bytesRead++] = q.poll(); while(!this.EOF && bytesRead < n) {
int read4Bytes = read4(read4Buffer);
if(read4Bytes < 4)
this.EOF = true;
int bytes = Math.min(n - bytesRead, read4Bytes);
System.arraycopy(read4Buffer, 0, buf, bytesRead, bytes);
bytesRead += bytes; if(bytes < 4) { //push read4 reminder to q for next read
for(int i = bytes; i < read4Bytes; i++)
this.q.offer(read4Buffer[i]);
}
} return bytesRead;
}
}

二刷:

这回题意理解得还算比较顺利。就是给一个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)。

/* The read4 API is defined in the parent class Reader4.
int read4(char[] buf); */ public class Solution extends Reader4 {
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
Queue<Character> remainingChars = new LinkedList<>(); public int read(char[] buf, int n) {
char[] read4Buf = new char[4];
int read4Count = 0;
int totalCharsRead = 0;
while (remainingChars.size() > 0 && totalCharsRead < n) {
buf[totalCharsRead++] = remainingChars.poll();
}
//if (totalCharsRead == n) return n;
while ((read4Count = read4(read4Buf)) > 0) {
int i = 0;
while (i < read4Count && totalCharsRead < n) {
buf[totalCharsRead++] = read4Buf[i++];
}
while (i < read4Count) {
remainingChars.offer(read4Buf[i++]);
}
}
return totalCharsRead;
}
}

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. 转:如何取得Spring管理的bean

    原文链接:http://blog.csdn.net/a9529lty/article/details/42145545 1.servlet方式加载时,[web.xml] <servlet> ...

  2. 15_会话技术_Cookie

    [简述] 会话可理解为:用户打开一个浏览器,点击多个超链接,访问服务器多个Web资源,然后关闭浏览器,整个过程成为一个会话. [会话过程中我们要解决的一些问题] * 每个用户与服务器进行交互的过程中, ...

  3. 用LinqToExcel处理有标题表格的数据

    1. 先根据表格标题定义一个类. public class News { public string Title { set; get; } public string Content { set; ...

  4. Oracle 关于定义约束 / 修改表结构 /修改约束

    ---约束分5种:主键 外键 唯一 非空 检查5类约束 Oracle中分列级别约束 与 表级别约束 列级别约束:在创建表时再列上面加约束 例如: create table table11( stuno ...

  5. 【转】循环递归遍历XML文档或按某要求遍历XML文档

    <?xml version="1.0" encoding="UTF-8"?> <catalog> <cd> <titl ...

  6. HTML5 canvas绘制雪花飘落动画(需求分析、知识点、程序编写分布详解)

    看到网上很多展示html5雪花飞动的效果,确实非常引人入胜,我相信大家也跟我一样看着心动的同时,也很好奇,想研究下代码如何实现:虽然哦很多地方也能下载这些源码,不过也不知道别人制作此类动画时的思路及难 ...

  7. 整理sed实战修改多行配置技巧

    老男孩老师有关sed实战技巧分享,来自课堂教学内容实战1.在指定行前插入两行内容,分别为oldboy和oldgirl.提示:被修改的文件内容必须要大于等于2行 1 sed -i '2 ioldboy\ ...

  8. python特性property

    通常,访问类和实例属性的时候,将返回所存储的相关值,也就是直接和类(实例的)的__dict__打交道.若果要规范这些访问和设值方式的话, 一种方法是数据描述符,另一种就是python内置的数据描述符协 ...

  9. 长达半年的苹果发布会:亮点与槽点(iPhone5s,iPhone5c)

    不知出于什么原因,今天凌晨召开的苹果发布会并没有视频直播,所以大家都守着The Verge家的图文直播.结果,苹果再一次用事实证明了他们没有保密体系,或者,故意没有保密体系. 整场发布会正经的亮点如下 ...

  10. js 转化类似这样的时间( /Date(1389060261000)/)问题

    首先在你的js文件里添加这段代码: /** * 日期时间格式化方法, * 可以格式化年.月.日.时.分.秒.周 **/ Date.prototype.Format = function (format ...