题目:

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 will only be called once for each test case.

链接: http://leetcode.com/problems/read-n-characters-given-read4/

题解:

英文不好,很难理解题意。简单讲就是已经有个API - read4(char[] buf),每次从文件里读取最多4个bytes到char[] buf里。要求根据read4来实现read(char[] buf, n),就是可以从文件中读取n个字符。这里我们要注意的就是,假如read4读取的字符数小于4,那么即到了文件尾部,我们可以使用一个变量EOF来记录这个时刻。除此之外就是一些判断和拷贝了。这里用到了System.arraycopy()。

Time Complexity - O(n),Space Complexity - O(1)

/* 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 int read(char[] buf, int n) {
char[] read4Buffer = new char[4];
boolean EOF = false;
int bytesRead = 0; while(!EOF && bytesRead < n) {
int read4Bytes = read4(read4Buffer);
if(read4Bytes < 4)
EOF = true;
int bytes = Math.min(n - bytesRead, read4Bytes);
System.arraycopy(read4Buffer, 0, buf, bytesRead, bytes);
bytesRead += bytes;
} return bytesRead;
}
}

二刷:

  1. 先建立一个read4Buf来保存使用read4 api之后读取的数据。 并且我们建立一个boolean EOF来代表是否读到了文件末尾,即read4返回的值小于4。
  2. 接下来我们用一个while循环来读取
  3. 当read4返回的值小于4的时候,我们设置EOF = true,即在下一次循环跳出
  4. 接下来我们来判断每次究竟要读取多少个bytes, 这个bytesToRead = Math.min(n - bytesRead,read4Bytes), 就是还剩多少字符要读取,以及read4 api的返回值里较小的一个
  5. 我们根据这个bytesToRead将read4Buf里的值拷贝到buf里,并且增加bytesRead
  6. 最后循环结束后,我们返回bytesRead就是我们究竟读取了多少字符。

Java:

Time Complexity - O(n),Space Complexity - O(1)

/* 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 int read(char[] buf, int n) {
char[] read4Buf = new char[4];
int bytesRead = 0;
boolean EOF = false;
while (!EOF && bytesRead <= n) {
int read4Bytes = read4(read4Buf);
if (read4Bytes < 4) {
EOF = true;
}
int bytesToRead = Math.min(n - bytesRead, read4Bytes);
for (int i = 0; i < bytesToRead; i++) {
buf[bytesRead++] = read4Buf[i];
}
}
return bytesRead;
}
}

三刷:

前面写得比较麻烦,这次换了新写法。我们首先创建一个read4Buf用来保存每次调用read4 api所返回的字符。建立两个变量,一个read4Count用来记录read4调用实际返回了多少个字符,另外一个totalCharRead表示我们总共已经读取了多少字符。使用一个while循环,在(read4Count = read4(read4Buf)) > 0的时候,每次拷贝这回read4调用读取的字符到输出buf中。注意拷贝时的条件是i < read4Count && totalCharRead < n

Java:

/* 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 int read(char[] buf, int n) {
char[] read4Buf = new char[4];
int totalCharRead = 0;
int read4Count = 0;
while ((read4Count = read4(read4Buf)) > 0) {
for (int i = 0; i < read4Count && totalCharRead < n; i++) {
buf[totalCharRead++] = read4Buf[i];
}
}
return totalCharRead;
}
}

Reference:

https://leetcode.com/discuss/19573/accepted-clean-java-solution

157. Read N Characters Given Read4的更多相关文章

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

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

  3. 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...

  4. ✡ leetcode 157. Read N Characters Given Read4 利用read4实现read --------- java

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

  5. 【LeetCode】157. Read N Characters Given Read4

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...

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

  8. Read N Characters Given Read4 I & II

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

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

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

随机推荐

  1. Linux下如何查看哪些端口处于监听状态

    查看某一端口的占用情况: lsof -i:端口号 前提:首先你必须知道,端口不是独立存在的,它是依附于进程的.某个进程开启,那么它对应的端口就开启了,进程关闭,则该端口也就关闭了.下次若某个进程再次开 ...

  2. RSA算法使用介绍

    http://www.cnblogs.com/AloneSword/p/3326750.html RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那 ...

  3. [转]如何学好windows c++编程 学习精髓(收集,整理)

    以下是很多VC爱好者的学习经历,希望对大家有所帮助: 我记得我在网上是这么说的:先学win32的SDK,也就是API, 再学MFC,这么一来呢,就先有个基础,MFC是API的封装, 如果API用的熟了 ...

  4. 解决VS报表.rdl 显示乱码“小方块”问题

    报表在编辑状态显示文本显示小方块 如图 原因:字体格式是英文状态下. 解决:选中文本框,选择文本框属性,选择字体,字体改成宋体或微软雅黑.就可以了.

  5. JAVA操作LDAP总结

    一.LDAP概念 LDAP的全称为Lightweight Directory Access Protocol(轻量级目录访问协议), 基于X.500标准, 支持 TCP/IP. LDAP目录为数据库, ...

  6. Headfirst设计模式的C++实现——状态模式(State)

    state.h #ifndef _STATE_H_ #define _STATE_H_ class GumballMachine; class State { public: ; ; ; ; Stat ...

  7. JS的词法作用域

    词法作用域定义实现的规则: 1 函数作用域实在定义的时候决定的,而不是在执行时候决定 2 为了实现这种词法作用域,函数内部不仅包含函数代码逻辑,还必须引用当前的作用域链. 3 函数对象可以通过作用域链 ...

  8. debian 学习记录-4 -关于linux -2

    来源:<Debian标准教程>王旭 著 Slackware.Debian.RedHat.SuSE 这4种发布版是当今大部分发布版的前去,虽然SuSE衍生自Slackware,但由于其技术变 ...

  9. java感触一则

    看到开源中国上边有那么多关于java的开源项目,从数据库到3D游戏再到IDE工具,甚至有iQQ,形形种种都是一些比较成熟的,工程很大的项目.才意识到Java是如此的强大和流行. 这么多开源的代码我不可 ...

  10. WPF 列表控件中的子控件上下文绑定

    <DataGrid Grid.ColumnSpan=" Height="Auto" SelectedItem="{Binding Path=SelectP ...