Difficulty: Hard

 More:【目录】LeetCode Java实现

Description

Similar to Question [Read N Characters Given Read4], but the read function may be called multiple times.

Intuition

题意:本题与上一题的区别就是连续多次调用read()函数,所以需要将存储4个字符的缓存buffer定义为全局变量,此外全局变量还需要定义buffer[]中的开始下标和缓存长度。本题中,需要注意的是,调用完一次read()函数后,可能没办法把buffer全部读完,所以要考虑到下一次调用read()函数时,对buffer的操作。详见代码。

Solution

public class Solution extends Reader4 {
private char[] buffer = new char[4];
int offset = 0, bufsize = 0; //buffer[]中的开始下标和缓存长度 /**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
public int read(char[] buf, int n) {
int readBytes=0; //已读的字符个数
boolean eof=false;
while(readBytes<n && !eof) {
if(bufsize==0) { //buffer[]中没有缓存了
bufsize=read4(buffer);
eof=(bufsize<4); //不能放到外面!
}
int bytes=Math.min(bufsize, n-readBytes);
System.arraycopy(buffer, offset, buf, readBytes, bytes);
offset=(offset+bytes)%4;
bufsize-=bytes;
readBytes+=bytes;
}
return readBytes;
}
}

  

What I've learned

1.

 More:【目录】LeetCode Java实现

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

  1. 【LeetCode】157. Read N Characters Given Read4

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...

  2. 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...

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

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

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

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

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

  7. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

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

  9. 【leetcode】955. Delete Columns to Make Sorted II

    题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...

随机推荐

  1. caffe 中 python 数据层

    caffe中大多数层用C++写成. 但是对于自己数据的输入要写对应的输入层,比如你要去图像中的一部分,不能用LMDB,或者你的label 需要特殊的标记. 这时候就需要用python 写一个输入层. ...

  2. Linux之安装虚拟机/虚拟操作系统[VisualBox]

    使用VisualBox安装虚拟机步骤: BIOS开启 Visual Technology(或者 visualization) 安装Visual Box 打开Visual Box > 新建虚拟电脑 ...

  3. DBeaver入门

    1 安装好连接好数据库,查询操作 注意黄色字体1 2 3 4 执行sql操作

  4. JavaScript学习 - 基础(七) - DOM event(事件)

    DOM event(事件) 定义事件: // 定义事件: //方式一,直接在标签上定义事件 // 方式二: var a11 = document.getElementsByName('a11')[0] ...

  5. java删除文件及其目录

    1.删除指定文件路径 public @ResponseBody String deleteFiles(HttpServletRequest request) { log.info(this.getCl ...

  6. map_server地图服务器

    http://wiki.ros.org/map_server 概述 map_server提供map_server ROS节点,它提供地图数据作为一个ROS服务器.也提供map_saver命令行功能,能 ...

  7. Check Box、Radio Button、Combo Box控件使用

    Check Box.Radio Button.Combo Box控件使用 使用控件的方法 1.拖动控件到对话框 2. 定义控件对应的变量(值变量或者控件变量) 3.响应控件各种消息 Check Box ...

  8. windows下caffe GPU版本配置

    由于项目需要,所以在自己本子上配置了一下windows下GPU版本的caffe; 硬件:  win10    ;      gtx1070独显(计算能力6.1): 安装软件:     cudnn-8. ...

  9. android摄像头(camera)之 v4l2的c测试代码【转】

    转自:https://blog.csdn.net/ldswfun/article/details/8745577 在移植android hal的过程中,移植的首要任务是要确保驱动完好,camera是属 ...

  10. 搭建RDA交叉编译器

    apt-get install subversion //安装版本控制系统,便于管理文件目录 apt-get install make atp-get install gcc =======set e ...