【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
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的更多相关文章
- 【LeetCode】157. Read N Characters Given Read4
Difficulty: Easy More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...
- 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
- ✡ 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 ...
- leetcode[158] Read N Characters Given Read4 II - Call multiple times
想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...
- [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 ...
- 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 ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- [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 ...
- 【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 ...
随机推荐
- node版本管理工具nvm安装以及使用
curl命令安装 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash 或者 使用wg ...
- Servlet3.0的注解自定义原生Listener监听器实战
简介:监听器介绍和Servlet3.0的注解自定义原生Listener监听器实战 自定义Listener(常用的监听器 servletContextListener.httpSessionListen ...
- 如何将SVN仓库转换为Git仓库
按如下步骤操作就可以将SVN仓库完整的转换为Git仓库: 1) 将远程SVN仓库搬到本地(这一步主要是为了提高转换的速度,也可以忽略) 参考这篇文章: http://rongjih.blog. ...
- JSON和JSONP详解
什么是JSON JSON是一种基于文本的数据交换方式,或者叫做数据描述格式,你是否该选用他首先肯定要关注它所拥有的优点. JSON的优点: 1.基于纯文本,跨平台传递极其简单: 2.Javascrip ...
- nagios系列(七)nagios通过自定义脚本的方式监控mysql主从同步
nagios监控mysql主从同步 起因:nagios可能监控到mysql服务的运行情况,但确不能监控mysql的主从复制是否正常:有时候,同步已经停止,但管理人员却不知道. 登陆mysql从服务器, ...
- TOMCAT服务器配置域名
最近做了个网站,用的是web'服务器是tomcat,框架式SpringMVC,功能做好后,就准备上线使用了, 手上已经有域名以及一台服务器,已经绑定好ip了,剩下的也就是配置Tomcat了,比较简单, ...
- ajax返回json对象的两种写法
1. 前言 dataType: 要求为String类型的参数,预期服务器返回的数据类型.如果不指定,JQuery将自动根据http包mime信息返回responseXML或responseText,并 ...
- Go语言规格说明书 之 通道类型(Channel types)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,介绍Go语言的 ...
- JS实现数组去重(重复的元素只保留一个)
1.遍历数组法 它是最简单的数组去重方法(indexOf方法) 实现思路:新建一个数组,遍历去要重的数组,当值不在新数组的时候(indexOf为-1)就加入该新数组中: ,,,,,,,,]; func ...
- 分享一些 Java 无关基础方面的书籍
个人认为看书有两个点好处: 1. 能出版出来的书一定是经过反复思考,雕琢和审核的,因此从专业性的角度来说,一本好书的价值超其他资料 2. 对着书上的代码自己敲的时候方便 “看完书之后再次提升自我的最好 ...