【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 ...
随机推荐
- 法律AI数据及应用
本文简单列举了法律AI目前的应用,数据集,研究方向. 历史 1970年,Buchanan和Headrick发表文章"关于人工智能和法律推理的一些猜测",讨论了对法律研究和推理进行建 ...
- 20155237 2016-2017-2 《Java程序设计》第8周学习总结
20155237 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 NIO与NIO2 认识NIO Channel: 衔接数据节点(与IO中的流对比) isOpe ...
- mysql 查询优化~sql优化通用
一 简介:今天我们来探讨下SQL语句的优化基础 二 基础规则: 一 通用: 1 避免索引字段使用函数 2 避免发生隐式转换 3 order by字段需要走索引,否则会发生filesor ...
- vue2.0环境安装
参考网站http://www.open-open.com/lib/view/open1476240930270.html (以上博客vue init webpack-simple 工程名字<工程 ...
- 攻打医院服务器的SamSam勒索木马分析
攻打医院服务器的SamSam勒索木马分析 近日一款名为SamSam的勒索木马在国外爆发.该木马利用医院系统的服务器漏洞实施入侵,再进行加密勒索钱财.由于医院网络信息安全水平普遍薄弱,SamSam成功感 ...
- 2、SpringBoot接口Http协议开发实战8节课(1-6)
1.SpringBoot2.xHTTP请求配置讲解 简介:SpringBoot2.xHTTP请求注解讲解和简化注解配置技巧 1.@RestController and @RequestMapping是 ...
- 如何调整cell的大小
一般情况下,我们使用tableview的时候从来没有设置过cell的大小(w,h).位置(x,y)等,而是系统直接给我们自动生成,但是有的时候我们可能会改动cell的大小及位置,比如:在适配ios6跟 ...
- Dom4j解析xml内容——(三)
Dom4j取标签中的内容用 getText ,取开始标签和结束标签之间的值. 取属性值有两种方式:
- Django 自定义过滤器和模板标签
前提:自定义模板标签和过滤器必须位于Django的某个应用中,这个应用可以包含一个templatetags目录, 和models.py views.py 处于同一级目录.若这个templatetags ...
- crontab定时作业
crontab用于在指定时间执行某项作业,如执行脚本或命令. 1.crontab -e 创建一个crontab文件,并添加作业(这个crontab的拥有者是创建者). 这里要注意:必须指定s ...