LeetCode Read N Characters Given Read4 II - Call multiple times
原题链接在这里:https://leetcode.com/problems/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 actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
By using the read4
API, implement the function int read(char *buf, int n)
that reads n characters from the file.
Note:
The read
function may be called multiple times.
题解:
需要多次调用,用queue来保存前一次调用read4没用完的数据.
read时先用queue中的数据添加到buf中,若是不够再call read4.
在读够n个char后若是read4Buff中还有可用数据,加到queue中.
Note: declear rest first, but not use i < n - readSum in the while condidtion since readSum is changing.
Time Complexity: read, O(n).
Space: O(1). queue的大小不会超过4.
AC Java:
/**
* The read4 API is defined in the parent class Reader4.
* int read4(char[] buf);
*/
public class Solution extends Reader4 {
LinkedList<Character> que = new LinkedList<>(); /**
* @param buf Destination buffer
* @param n Number of characters to read
* @return The number of actual characters read
*/
public int read(char[] buf, int n) {
int readSum = 0;
// 先用queue中剩余的上次结果加到buf中
while(readSum < n && !que.isEmpty()){
buf[readSum++] = que.poll();
} // 若是不够再调用read4 API
boolean eof = false;
char [] temp = new char[4];
while(!eof && readSum < n){
int count = read4(temp);
eof = count < 4;
int rest = n-readSum; int i = 0;
while(i < count && i < rest){
buf[readSum++] = temp[i++];
} // 把当前read4Buff中没有读的有用char加到queue中
if(i == rest){
while(i < count){
que.add(temp[i++]);
}
}
} return readSum;
}
}
类似Read N Characters Given Read4.
LeetCode Read N Characters Given Read4 II - Call multiple times的更多相关文章
- [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- [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】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- ✡ 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 ...
- 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 actu ...
- [LeetCode] Read N Characters Given Read4 I & II
Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...
随机推荐
- soapui中文操作手册(四)----MOCK服务
Web Service Mocking是武器库一个非常有用的工具.这是解决“如果没有Web服务如何创建针对性的Web服务测试”问题的办法.Web Service Mocking将在这里派上用场.它允许 ...
- Darkest page of my coding life
The code i wrote a while ago recently caused a disaster and as I reviewed it I found it is the silli ...
- Codeforces Round #216 (Div. 2)A. Valera and Plates
#include <iostream> using namespace std; int main(){ int n, m , k; cin >> n >> m & ...
- SRM 595 DIV1 250
挺简单的组合把. #include <cstdio> #include <cstring> #include <iostream> #include <vec ...
- db2日常维护
一. DB2日常维护操作 1.数据库的启动.停止.激活 db2 list active databases db2 active db 数据库名 db2start --启动 db2stop [forc ...
- Sping Environment为Null的原因和解决方法
参考:https://github.com/spring-projects/spring-boot/issues/4711 这个issue提出不到20天给我搜出来了,还是相信google的强大 问题: ...
- jquery插件之文字间歇自动向上滚动
该插件乃本博客作者所写,目的在于提升作者的js能力,也给一些js菜鸟在使用插件时提供一些便利,老鸟就悠然地飞过吧. 此插件旨在实现目前较为流行的文字间歇向上滚动特效,当鼠标移动到文字上时,向上滚动会停 ...
- Java中的super与this解析
好了,现在开始讨论this&super这两个关键字的意义和用法. 在Java中,this通常指当前对象,super则指父类的.当你想要引用当前对象的某种东西,比如当前对象的某个方法,或当前对象 ...
- mysqlbinglog基于即时点还原
mysqlbinlog介绍 要想从二进制日志恢复数据,你需要知道当前二进制日志文件的路径和文件名.一般可以从选项文件(即my.cnf or my.ini,取决于你的系统)中找到路径. (mysql5. ...
- HTML5初学篇章_3
表单的标签是<form>,它使页面与客户的互动成为可能.而它的大部分元素字自HTML2.0后就没有再改变过,由此可见这是一个多么具有卓越性的设计. <form>标签是用于创建供 ...