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. osharp3使用经验:整合DbContextScope 文章 1

    osharp3的事务处理是跳过savechangeing方法来控制的,没有DbContextScope专业 DbContextScope管理dbcontext的优劣本文不讨论 整合过程: 1.在.Da ...

  2. Jsp与servlet的区别 1

     Jsp与servlet的区别 2011-12-09 16:27:47 分类: Java 1.jsp经编译后就变成了Servlet.(JSP的本质就是Servlet,JVM只能识别java的类,不能识 ...

  3. Java调试

    线上load高的问题排查步骤是: 先用top找到耗资源的进程 ps+grep找到对应的java进程/线程 jstack分析哪些线程阻塞了,阻塞在哪里 jstat看看FullGC频率 jmap看看有没有 ...

  4. Quartz.Net 基于XML配置启动

    1.App.config <configSections> <section name="quartz" type="System.Configurat ...

  5. Jetty多Connector

    有时候想要启动两个端口,或者通过一个Jetty server提供多个不同服务,比如说使用8080来指定默认访问端口,使用8433指定https访问端口等等,此时就可以通过创建多个Connector来解 ...

  6. Java使用Jetty实现嵌入式Web服务器及Servlet容器

     Jetty是一个Java实现的开源的servlet容器,它既可以像Tomcat一样作为一个完整的Web服务器和Servlet容器,同时也可以嵌入在Java应用程序中,在Java程序中调用Jetty. ...

  7. MD5 (摘要加密)

    MD5 约定 同样的密码,同样的加密算法,每次加密的结果是不一样 密码方案 方案一:直接 MD5 pwd = pwd.md5String; 非常不安全 方案二 MD5 + 盐 pwd = [pwd s ...

  8. firefox如何卸载插件plugins和临时文件夹

    下载原版的 英文版的 firefox 会看到 openH264 video codec Plugin和microsoft DRM (digit rightcopy manager 数字版权管理)等等插 ...

  9. iframe和frameset的使用

    iframe是:inner frame的缩写, 必须指明src属性,不能直接在里面写内容, 否则不会显示. 可以载入html, *.图片文件, txt文件等等. html的属性中,表示长度高度的尺寸属 ...

  10. jQuery 2.0发布,不再支持IE6/7/8

    有时发现jQuery库引用的都对,javascript代码写的也没问题,可是jquery就是出现问题,额--我发现换个jquery库就没问题了,长时间不关注jquery的问题而已: 很多人都没有使用最 ...