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 ...
随机推荐
- linux建立ssh信任关系
一.建立SSH信任将A主机做为客户端(发起SSH请求 ip:192.168.200.170)将B主机作为服务器端(接收ssh请求 ip:192.168.200.149)以上以主动发起SSH登录请求 ...
- 【BZOJ】1100: [POI2007]对称轴osi
题意 给一个\(n(1 \le n \le 100000)\)个点不自交的多边形,求对称轴数目. 分析 将多边形表示成长度和角的形式(用有向面积来表示角也行),然后匹配. 题解 匹配可以用kmp或ma ...
- 【CodeVS】 p1225 八数码难题
题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.问题描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字 ...
- linux 下如何打开core dump文件开关
dump文件可以在程序crash时,方便我们查看程序crash的地方和上下文信息.在window下,要能生成dump文件,需要自己编写相应的代码.不过现在网上可以找到相应的代码,只要把它下载后然后加到 ...
- AngularJS 乱记
1. 前端简单逻辑 <title data-ng-bind="{true:' ('+notice_count+') '}[notice_count > 0]+{true:glob ...
- Windows Phone Foreground Toast
Basically ToastPrompt is an UI component that derives from the Coding4Fun toolkit's abstract PopUp&l ...
- 用atom写LaTeX文档
下载并安装Tex Live: 下载页面 下载并安装atom:下载页面 打开atom File -> Settings -> Install 搜索并安装: language-latex la ...
- TLV简介
引子: 前段时间在项目中第一次接触TLV,项目中用这种格式来传输图片,语音等. 关于TLV TLV是一种可变的格式,意为:Type类型, Lenght长度,Value值.Type:该字段是关于标签和编 ...
- [LintCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 单页Web应用:
概念: Web应用程序: WEB应用程序一般是B(浏览器)/S(服务器)模式.Web应用程序首先是“应用程序”,和用标准的程序语言,如C.C++等编写出来的程序没有什么本质上的不同.然而Web应用程序 ...