博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Read N Characters Given Read4 II - Call multiple times
阅读量:7029 次
发布时间:2019-06-28

本文共 2442 字,大约阅读时间需要 8 分钟。

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.Show TagsHave you met this question in a real interview?

这道题跟I不一样在于,read函数可能多次调用,比如read(buf,23)之后又read(buf, 25), 第一次调用时的buffer还没用完,还剩一个char在buffer里,第二次拿出来接着用,这样才能保证接着上次读的地方继续往下读。

1. 所以应该设置这4个char的buffer为instance variable(实例变量),这样每次调用read函数后buffer还剩的元素可以被保存下来,供给下一次读取

2. 那么下一次调用read函数时候,怎么知道上一次buffer里面还剩不剩未读元素呢?我们有oneRead(一次读到buffer里的char数),actRead(实际被读取的char数),oneRead-actRead就是还剩在buffer里的char数。通常oneRead == actRead, 只有当n-haveRead < oneRead时,才不等,这就是上一次调用read结束的时候。所以只要调用read函数发现oneRead != 0 时,就说明上一次调用read还剩了元素在buffer里,先读完这个,再调用read4读新的。oneRead也需要是instance varaible

3. 还需要设置一个offset: Use to keep track of the offset index where the data begins in the nextread call. The buffer could be read partially (due to constraints of reading upto n bytes) and therefore leaving some data behind.

                      |<--buffer-->|   

    // |_________________________|
    // |<---offset---> |<----oneRead--->

上图所示为一次read最后的情况,offset部分其实就是actRead的部分,oneRead = oneRead - actRead, 就剩下了右边一部分在buffer里没有读,下一次read函数调用,发现oneRead>0, 说明上一次read还剩了一部分没有读。oneRead表示的其实就是上一次读剩下的char数,offset表示这一次读应该开始的位置

其实上图的oneRead不一定会充满整个右边部分的,有可能上一次读oneRead根本没有读满整个buffer。 所以oneRead+offset并不一定等于整个buffer。这也就是为什么我们一定要用两个变量oneRead\offset的原因,因为oneRead并不一定=4-offset

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      private char buffer = new char[4];11      private int oneRead = 0;12      private int offset = 0;13      14      public int read(char[] buf, int n) {15          boolean lessthan4 = false;16          int haveRead = 0;17          while (!lessthan4 && haveRead < n) {18              if (oneRead == 0) {19                  oneRead = read4(buffer);20                  lessthan4 = oneRead < 4;21              }22              int actRead = Math.min(n-haveRead, oneRead);23              for (int i=0; i

 其实我觉得可以只用一个offset作为instance variable,而不用再用一个oneRead, 因为offset+oneRead = 4

转载地址:http://xygxl.baihongyu.com/

你可能感兴趣的文章
javascript深入理解js闭包(转)
查看>>
207. Course Schedule
查看>>
如何优化您的 Android 应用 (Go 版)
查看>>
Trie树实现
查看>>
Opencv无法调用cvCaptureFromCAM无法打开电脑自带摄像头
查看>>
Exception异常处理机制
查看>>
复杂的web---web中B/S网络架构
查看>>
编写文档的时候各种问题
查看>>
Eclipse里maven的project报Unbound classpath variable: 'M2_REPO/**/***/***.jar
查看>>
新旅程CSS 基础篇分享一
查看>>
查看内核函数调用的调试方法【原创】
查看>>
个人项目中遇到的问题
查看>>
byte与base64string的相互转化以及加密算法
查看>>
20145103 《Java程序设计》第3周学习总结
查看>>
ubuntu声音系统
查看>>
哈哈更新资源列表2
查看>>
冲刺第一周第五天
查看>>
Java 接口
查看>>
Android 微信第三方登录
查看>>
硬盘的读写原理
查看>>