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. ListViewDemo

    ListView Layout示例:MainActivity.java中定义待显示的数据countryArray, 在activity_main中定义ListView,activity_listvie ...

  2. js对象,原型,call,apply浅析

    //对象直接量,创建对象最简单的方式是在js里使用对象直接量 var book = { "main title": "js", //属性里有空格,要用引号 &q ...

  3. Windows SDK 之 Hook的使用

    在使用SetWindowsHookEx的过程中遇到的问题 函数原型 HHOOK WINAPI SetWindowsHookEx( _In_ int idHook, _In_ HOOKPROC lpfn ...

  4. jquery $.trim()方法使用介绍

    $.trim(str)的作用是去掉字符串首尾空格 $.trim(str) 返回:string: 说明:去掉字符串首尾空格. 示例: 先看个错误代码: <input type="text ...

  5. JQuery之append和appendTo的区别,还有js中的appendChild用法

    JQuery之append和appendTo的区别 append()前面是要选择的对象,后面是要在对象内插入的元素内容 appendTo()前面是要插入的元素内容且为Jquery对象,而后面是要选择的 ...

  6. Objective-C 【电商APP应用代码-系统分析-详细注释-代码实现】

    ------------------------------------------- 电商APP应用 ************************************************ ...

  7. socket.io问题,io.sockets.manager.rooms和io.sockets.clients('particular room')这两个函数怎么用?

    为什么我用nodejs用这个两个函数获取都会出错呢?是不是socket的api改了?请问现在获取房间数和当前房间的客户怎么获取?什么函数?谢谢!!急求!     网友采纳 版本问题.io.socket ...

  8. 【Unity3D】场景转换与退出

    1.场景转换 : 老版本的场景切换用的是Application.LoadLevel([场景名字或者在File->Build settings里面的场景代号]); 新版本的场景转换用到了Scene ...

  9. C#代码配置IIS 操纵IIS

    前言: IIS到目前经历了四个版本分别为 IIS4.0  IIS5.0 IIS6.0 IIS7.0,其中IIS6.0 IIS7.0是在5.0的安全问题的基础上获得的发展,目前为止.6.0版本以后的都是 ...

  10. WCF之契约

    消息交换的双方,为了进行消息交换,而定义的一些数据交换规则,称之为契约. 契约只约束规则,不管实现. 契约对客户端和服务器的要求. 服务器:定义和实现契约.构建ServiceHost实例,然后暴露En ...