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. Python之路【第七篇】:初识Socket

    What is Socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制, ...

  2. jQuery 参考手册 - 遍历

    jQuery 参考手册 - 遍历 jQuery Ajax jQuery 数据 jQuery 遍历函数 jQuery 遍历函数包括了用于筛选.查找和串联元素的方法. 函数描述 .add()将元素添加到匹 ...

  3. 如何提高MySQL Limit查询的性能

    MYSQL的优化是非常重要的.其他最常用也最需要优化的就是limit.mysql的limit给分页带来了极大的方便,但数据量一大的时候,limit的性能就急剧下降. 同样是取10条数据 select ...

  4. Hadoop之HDFS文件操作常有两种方式(转载)

    摘要:Hadoop之HDFS文件操作常有两种方式,命令行方式和JavaAPI方式.本文介绍如何利用这两种方式对HDFS文件进行操作. 关键词:HDFS文件    命令行     Java API HD ...

  5. javascript 数据类型转换

    javascrīpt 类型转换函数 在Javascrīpt中,Double类型和Int类型都是看作为Number对象. 1.Number转成String number.toString() Strin ...

  6. jelq

    初级 The Newbie Routine 5 minutes hot wrap 5 minutes manual stretch (ten 30-second stretches) 10 minut ...

  7. log4net的基本配置及用法

    [1].[代码] [C#]代码 跳至 [1] [2] ? 1 2 using System.Reflection;  //使用反射 static private ILog log = log4net. ...

  8. 清北学堂模拟day4 捡金币

    [问题描述]小空正在玩一个叫做捡金币的游戏.游戏在一个被划分成 n行 n列的网格状场地中进行.每一个格子中都放着若干金币,并且金币的数量会随着时间而不断变化. 小空的任务就是在网格中移动,拾取尽量多的 ...

  9. PHP基础OOP(一)

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  10. <s:url>指向的Action只执行一次,清除浏览器缓存文件后又可执行一次。

    Action中的方法仅为静态变量赋值,而其他访问数据库的Action可以被重复执行. 起初判断可能是静态变量的内存机制导致不能重复执行. 然后发现清楚浏览器缓存文件后又可以执行一次了,看来原因在Jsp ...