【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 ...
随机推荐
- JavaScript之数值计算
//两等长数组对应元素之间做减法运算[可拓展:基本运算(+/-*//)] function array_dif(length,arrayA,arrayB){ var array = new Array ...
- TCP网络编程
TCP网络编程 与UDP不同的是TCP是通过客服端和服务端的方式来传输数据的.客服端:public class TCPClient { /** * @param args * @th ...
- 列式数据库~clickhouse 副本集架构的搭建
clickhouse 搭建副本集 一 原理: 1 依赖ZK,ZK的基础上,ZK存储数据库元数据 2 使用复制表引擎创建复制表,包括ZK路径和副本名,相同ZK路径的表可以相互复制 3 复制表本身拥 ...
- Python 入门基础17 --加密、表格、xml模块
今日内容: 1.hashlib模块:加密 2.hmac模块:加密 3.configparser模块:操作配置文件 4.subprocess模块:操作shell命令 5.xlrd模块:excel 6.x ...
- 2018-2019-2 网络对抗技术 20165320 Exp5 MSF基础应用
2018-2019-2 网络对抗技术 20165320 Exp5 MSF基础应用 一.实践内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 一个主 ...
- 代码控制打电话、发短信、发邮件、打开手机app等操作
很多时候我们需要利用我门自己的app进行一些打电话.发短信等的操作,那么如何利用代码实现呢,下面就介绍一些简单的方法来实现这些操作. 一.打电话: <1>最简单.最直接的方法----直接跳 ...
- Apriori 算法python实现
1. Apriori算法简介 Apriori算法是挖掘布尔关联规则频繁项集的算法.Apriori算法利用频繁项集性质的先验知识,通过逐层搜索的迭代方法,即将K-项集用于探察(k+1)项集,来穷尽数据集 ...
- zabbix通过简单命令监控elasticsearch集群状态
简单命令监控elasticsearch集群状态 原理: 使用curl命令模拟访问任意一个es节点可以反馈的集群状态,集群的状态需要为green curl -sXGET http://serverip: ...
- 网站遭遇CC及DDOS攻击紧急处理方案
检测访问是否是CC攻击的命令: 80口为网站的访问端口,可以根据实际情况进行修改 # netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: ' ...
- Android数据存储:Shared Preferences
Android数据存储之SharedPreferences 在Android系统中提供了多种存储技术.通过这些存储技术可以将数据存储在各种存储介质上, Android 为数据存储提供了如下几种方式:1 ...