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. 拿webshell权限方法(一)

    概念 Webshell就是以asp,php,jsp或cgi等网页文件形式存在的一种命令执行环境,也可以将其称为一种网页后门. Access数据库拿webshell 首先看是否能找到网站漏洞进行网站的后 ...

  2. ASP.NET MVC中的Session设置

    最近在ASP.NET MVC项目中碰到这样的情况:在一个controller中设置了Session,但在另一个controller的构造函数中无法获取该Session,会报"System.N ...

  3. 原 layer父子页面交互

    1.访问父页面元素值 2.访问父页面方法 3.如何关闭弹出的子页面窗口 parent.layer.close(index);//关闭弹出的子页面窗口 4.如何从子页面执行刷新父页面操作 [javasc ...

  4. 【逆向工具】IDA使用6-签名文件制作

    0x1 签名文件制作的方法: 找到静态编译的程序库 使用IDA中的fair工具包,对静态库操作,生成特征库(IDA6.8 是flair68.zip) 0x2 步骤 第一步:使用pcf生成对应静态库的p ...

  5. ubuntu 禁用 guest 账户

    第一步: run the command(s) below:        (编辑如下文件) sudo vi /usr/share/lightdm/lightdm.conf.d/50-ubuntu.c ...

  6. [转]解决win7 64位操作系统下安装PL/SQL后连接报错问题: make sure you have the 32 bits oracle client installed

    1. 在Oracle官网(http://www.oracle.com/technetwork/topics/winsoft-085727.html)下载文件: instantclient-basic- ...

  7. git强制覆盖更新

    1.选择项目,右键选择Team,选择Team里面的Reset. 2.点击Reset后弹出Reset窗口,选择Hard选项,这样就会把远端的项目完全拉下来覆盖本地,本地没有的文件也会被创建,点击确定.

  8. java项目中oracle配置说明

    配置信息: #oracle database settings jdbc.url::orcl jdbc.username=cognos_data jdbc.password=cognos_data j ...

  9. bzoj 1803: Spoj1487 Query on a tree III(主席树)

    题意 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点) 假设没有两个相同的点权. 输入格式: 第一行为整数n, ...

  10. js实现弹窗居中

    在一些页面中,我们总会遇到一些弹窗不居中的时候,还要根据浏览器的大小来调整弹窗的弹出位置, 之前我也遇到这样的问题,现在我把我知道的呈现给大家 css样式 .windowBox{ width:500p ...