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 only once.

 public class Solution extends Reader4 {
public int read(char[] buf, int n) {
int index = ;
char[] tmp = new char[]; // temp buffer while (index < n) {
int count = read4(tmp);
count = Math.min(count, n - index);
for (int i = ; i < count; i++) {
buf[index++] = tmp[i];
}
if (count < ) {
return index;
}
}
return index;
}
}
 public class Solution extends Reader4 {
public int read(char[] buf, int n) {
boolean eof = false;
int charsRead = ;
char[] buf4 = new char[]; while (!eof && charsRead < n) {
int size = read4(buf4);
if (size < ) {
eof = true;
} if (charsRead + size > n) {
size = n - charsRead;
} System.arraycopy(buf4, , buf, charsRead, size);
charsRead += size;
}
return charsRead;
}
}

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.

分析:

 public class Solution extends Reader4 {
char[] buffer = new char[];
int bufferSize = , prevIndex = ; public int read(char[] buf, int n) {
int index = ;
while (index < n) {
if (prevIndex < bufferSize) {
buf[index++] = buffer[prevIndex++];
} else {
bufferSize = read4(buffer);
prevIndex = ;
if (bufferSize == ) { break; }
}
}
return index;
}
}
 public class Solution extends Reader4 {
private char[] buffer = new char[];
int offset = , bufsize = ; public int read(char[] buf, int n) {
int readBytes = ;
boolean eof = false;
while (!eof && readBytes < n) {
int sz = (bufsize > ) ? bufsize : read4(buffer);
if (bufsize == && sz < ) eof = true;
int bytes = Math.min(n - readBytes, sz);
System.arraycopy(buffer, offset, buf, readBytes, bytes);
offset = (offset + bytes) % ;
bufsize = sz - bytes;
readBytes += bytes;
}
return readBytes;
}
}

Read N Characters Given Read4 I & II的更多相关文章

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

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

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

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

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

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

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

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

  7. LeetCode Read N Characters Given Read4

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

  8. [LeetCode] 157. 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 ...

  9. [LeetCode#157] Read N Characters Given Read4

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

随机推荐

  1. Ubuntu用作Server时出现乱码的解决方法

    下面给出解决办法: 1.用vi编辑器修改/etc/default/local文件 2.把原来的中文编码替换成下面的 LANG="en_US.UTF-8" LANGUAGE=&quo ...

  2. Python之路【第五篇续】:面向对象编程二

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABgQAAALaCAIAAABxja8cAAAgAElEQVR4nOzd6X9Tdd74/+uv+f5uzF

  3. Mac截图快捷键

    Shift+Command+3 截取全屏幕至桌面 Shift+Command+4 截取部分屏幕至桌面 Shift+Command+4+空格 截取窗口或原件至桌面 Shift+Command+4 然后E ...

  4. Mysql 常用函数

    统计函数: count()   统计记录条数,如 select count(*) from stu; sum()     统计记录字段的和,如select sum(salary) from emp; ...

  5. Junit使用教程 转

    几乎所有程序员都听说过Junit的大名,但不知真正懂得运用它的人有多少,我便是其中的一个小白. 知道Junit是用来测试的,但却把“宝刀”当成了“菜刀”用.为了从此不再菜鸟,特此总结整理了下Junit ...

  6. 《Effective C++》第三版笔记

    阅读此笔记前,请先阅读 <Effective C++>第二版笔记  和  <More Effective C++>笔记 这里只记录与上面笔记不同的条款,主要是 "面对 ...

  7. Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法

    方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFEST.MF”, 由于是打包引用了第三方jar包的Java项目,故需要自定义配置文件MANIFEST.MF,在该项目 ...

  8. 谈谈我眼中的CSDN吧

    昨天逛博客园看到了这篇曝光率很高的文章:博客搬家——从CSDN到博客园,一篇短短的文章竟然招致这么多人的讨论,可能程序员就喜欢“Java好还是PHP好”这类型的问题吧,好无聊.由于我一直在使用CSDN ...

  9. 密码学初级教程(一)基本概念及DES加密算法

    密码技术在网络通信中广泛使用,本节是初步接触密码学技术的笔记. 第1章 加密-解密 破译 明文-密文 密钥 密码算法 对称密码-公钥密码(非对称密码) 单向散列函数-散列值 消息认证码 数字签名 伪随 ...

  10. Android手机同步电脑端google chrome书签

    我先声明:文中FQ 都是博客园自动将中文(fan qiang)转换为FQ的,并不是我本来写的就是FQ~~ 手机和电脑都必须要能登录google(Xee:几乎所有做开发的人都每天的生活都离不开谷歌了,可 ...