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.

 /* 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
*/
private char[] tmp = new char[4];
private int tmpPoint; //indicate which position we should start reading
private int tmpContain;//indicate how many char in the tmp buff
public int read(char[] buf, int n) {
int cur = 0;
while(cur < n){
if(tmpPoint == 0){
tmpContain = read4(tmp);
}
if(tmpContain == 0){
return cur;
}
while(cur < n && tmpPoint < tmpContain){
buf[cur ++] = tmp[tmpPoint ++];
}
tmpPoint = tmpPoint % tmpContain;
}
return cur;
}
}

Read N Characters Given Read4 II - Call multiple times的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

  9. Leetcode-Read N Characters Given Read4 II

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

随机推荐

  1. 【BZOJ1053】[HAOI2007]反素数

    [BZOJ1053][HAOI2007]反素数 题面 bzoj 洛谷 题解 可以从反素数的定义看出小于等于\(x\)的最大反素数一定是约数个数最多且最小的那个 可以枚举所有的质因数来求反素数,但还是跑 ...

  2. [算法]用java实现堆操作

    问题描述:(1)建堆:将数组A[1..n]变成一个最大堆.(课本6.3)(2)堆排序:将一个堆中的元素按递减排序输出.(3)用插入方法建堆:堆大小从1到n每次插入一个元素到堆中,直到n个元素入堆.(课 ...

  3. js 删除url指定参数

    /** * 删除当前url中指定参数 * @param names 数组或字符串 * @returns {string} */ function funcUrlDel(names) { if(type ...

  4. Field 'email' doesn't have a default value

    MySQL在出现这个Field xxx doesn't have a default value错误的原因是:我们设置了该字段为非空,但是我们没有设置默认值造成的. 或着 缺少字段.

  5. h5 和之前版本的区别

    html5和之前版本的区别就是:以前版本多采用<tr><td>等标签,对于webapp的开发不是很好把控.H5采用<div>等标签直接进行布局(且多了许多标签功能很 ...

  6. How to use the windows active directory to authenticate user via logon form 如何自定义权限系统,使用 active directory验证用户登录

    https://www.devexpress.com/Support/Center/Question/Details/Q345615/how-to-use-the-windows-active-dir ...

  7. Java中的Union Types和Intersection Types

    前言 Union Type和Intersection Type都是将多个类型结合起来的一个等价的"类型",它们并非是实际存在的类型. Union Type Union type(联 ...

  8. Android Dalvik虚拟机初识

    摘自:http://blog.csdn.net/andyxm/article/details/6126907 首先,让我们来思考下面几个问题: 什么是Dalvik虚拟机? Dalvik VM与JVM有 ...

  9. 拒绝滥用golang defer机制

    原文链接 : http://www.bugclosed.com/post/17 defer机制 go语言中的defer提供了在函数返回前执行操作的机制,在需要资源回收的场景非常方便易用(比如文件关闭, ...

  10. 1、Ansible安装配置

    ansible介绍: Ansible是一款基于Python开发的自动化运维工具,主要是实现批量系统配置.批量程序部署.批量运行命令.批量执行任务等等诸多功能.Ansible是一款灵活的开源工具,能够很 ...