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.

Analysis:

Since the function can be called multiple times, we need to record the left over content in the buffer of read4, and put them in some place. For the next call, we need to read content from the carry over buffer first.

Solution:

 /* The read4 API is defined in the parent class Reader4.
int read4(char[] buf); */ public class Solution extends Reader4 {
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/ public char[] carry;
public int index; public int read(char[] buf, int n) {
int left = n; //Put carry over into buf, and destroy the carry over array!
if (carry!=null){
while (left>0 && index<carry.length){
buf[n-left]=carry[index];
left--;
index++;
}
if (index>=carry.length){
carry=null;
index=-1;
}
} char[] tempBuf = new char[4];
while (left>0){
int num = read4(tempBuf);
//if the read number is larger then what we need, then we just put the left number of chars into buf.
//And put the rest chars into carry array.
if (num>left){
carry = new char[num-left];
for (int i=left;i<num;i++)
carry[i-left] = tempBuf[i];
index = 0;
} int end = Math.min(num,left);
for (int i=0;i<end;i++){
buf[n-left] = tempBuf[i];
left--;
} //If reach EOF.
if (left>0 && num<4) break;
} return n-left; }
}

Leetcode-Read N Characters Given Read4 II的更多相关文章

  1. [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 ...

  2. LeetCode Read N Characters Given Read4 II - Call multiple times

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...

  3. [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 ...

  4. [LeetCode] Read N Characters Given Read4 用Read4来读取N个字符

    The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...

  5. LeetCode Read N Characters Given Read4

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...

  6. [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 ...

  7. 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...

  8. ✡ 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 ...

  9. leetcode[158] Read N Characters Given Read4 II - Call multiple times

    想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...

  10. [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 ...

随机推荐

  1. Javascript常见操作

    图片预加载 var image = new Image();image.onload = onLoad;image.onerror = onLoad;image.src =src; image.com ...

  2. Abp Zero——前端如何新增功能模块

    为适应不同开发人员,abp rezo的UI实现了spa和mpa两套: spa--Single-page Application(单页面应用),默认"http://localhost/Acco ...

  3. Part 12 DateTime functions in SQL Server

    DateTime functions in SQL Server IsDate, Day, Month, Year and DateName DateTime functions in SQL Ser ...

  4. XenApp简单部署

    作者:MR.Yangwj 目录 XenApp简单部署... 1 一.         XenApp安装... 1 (一)      服务器配置任务... 9 1)     许可证服务器配置... 9 ...

  5. ubuntu启动失败the system is running in low graphics mode

    ubuntu启动失败the system is running in lowg raphics mode 起因 ubuntu重新设置selinux的模式 修改配置文件/etc/selinux/conf ...

  6. git 基本使用

    简单几步操作让你在终端下用git实现文件的上传. 一.克隆项目    在工作中,常见的情景都是远程库已经建好了,需要大家把代码拉下来,共同协作开发.本文所有操作均在终端下进行.    //克隆一个本地 ...

  7. Firebug 调试技巧之console API

    console.log(object[, object, ...]) Writes a message to the console. You may pass as many arguments a ...

  8. spring4.0源码导入

    一个面试,让我知道了自己的不足,一天不进步就是倒退. spring源码导入eclipse 本人的环境 (我导入的是最新的spring 4.0 所以要用jdk1.8) 1 安装git (mac上自带了g ...

  9. xmlspy注册后打开报错的解决办法

    XMLSpy 2011中文版破解补丁使用方法 1.如果你下载的版本是r2sp1的话(r2不用此步骤),先用补丁主程序(altova.xmlspy.v2011r2sp1b-patch.exe).2.XM ...

  10. Lucene Field

    org.apache.lucene.demo.IndexFiles类中,使用递归的方式去索引文件.在构造了一个IndexWriter索引器之后,就可以向索引器中添加Doucument了,执行真正地建立 ...