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中文操作手册(一)----创建一个新的项目
1) 创建一个新的项目 点击项目,选择新建项目SOAP.这将打开一个新的SOAP项目对话框. 注意:你也可以做CTRL + N(WIN)或CMD+ N(MAC)来创建一个新的SOAP项目. 在新的SO ...
- BZOJ3934 : [CQOI2015]标识设计
轮廓线插头DP. 设$f[i][j][a][b][c][d][e]$表示考虑到了$(i,j)$,轮廓线上3个下插头的位置分别为$a,b,c$,是否有右插头,已经放了$e$个$L$的方案数. 然后直接D ...
- (转)linux命令行下的ftp 多文件下载和目录下载
link:http://yahoon.blog.51cto.com/13184/200991 目标ftp服务器是一个非标准端口的ftp 1.通过shell登录 #ftp //shell下输入 ...
- Android 读取蓝牙设备信息开发
(1)Android手机一般以客户端的角色主动连接SPP协议设备(接上蓝牙模块的数字传感器),连接流程是: 1.使用registerReceiver注册BroadcastReceiver来获取蓝牙状态 ...
- android pcm
Android.media package里包含声音录放的两个类AudioRecord和AudioTrack.前者用来录制,后者用来播放. 配置 pcm: int channel = AudioFor ...
- Codeforces Round #250 (Div. 2) C、The Child and Toy
注意此题,每一个部分都有一个能量值v[i],他移除第i部分所需的能量是v[f[1]]+v[f[2]]+...+v[f[k]],其中f[1],f[2],...,f[k]是与i直接相连(且还未被移除)的部 ...
- ACM 对决
对决 时间限制:1000 ms | 内存限制:65535 KB 难度:0 描述 Topcoder要招进来了 n 个新同学,Yougth计划把这个n个同学分成两组,要求每组中每个人必须跟另一组中 ...
- ACM: 强化训练-海贼王之伟大航路-dfs-枝减
海贼王之伟大航路 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descriptio ...
- 移动开发流量省起来之Zepto
一张图说明Zepto.js的优势: jquery 1.x最新版284KB,压缩后94KB:jquery2.x最新版247KB,压缩后84KB:Zepto最新版54KB,压缩后9KB!!! 然后 ...
- 51nod算法马拉松13
A 取余最长路 不难发现路径可以拆成三条线段,只要知道两个转折点的位置就能计算出答案. 设sum(i,l,r)表示第i行从l到r元素的和,则答案可以表示为sum(1,1,x)+sum(2,x,y)+s ...