157. Read N Characters Given Read4

时间:2022-04-08 09:01:42

题目:

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;
}
}

157. Read N Characters Given Read4

Reference:

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

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

  1. &lbrack;LeetCode&num;157&rsqb; 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. &lbrack;LeetCode&rsqb; 157&period; 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&period; Read N Characters Given Read4 解题报告&lpar;C&plus;&plus;&rpar;

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

  4. ✡ leetcode 157&period; 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&period; Read N Characters Given Read4

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

  6. &lbrack;LeetCode&rsqb; 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. &lbrack;LeetCode&rsqb; 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 &amp&semi; 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. java 解析并生成 XML

    在 java 中使用 Dom4j 解析 XML 对 XML 文件的解析,通常使用的是 Dom4j 和 jdom 作为XML解析工具. 在此只介绍下 Dom4j 对 XML 文件的解析使用方法. 1. ...

  2. LeetCode Binary Tree Level Order Traversal II &lpar;二叉树颠倒层序&rpar;

    题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...

  3. runtime error &quest;

    程序运行时错误(运行时出错就是出现在程序运行过程中的),有很多种: 比如:溢出.内存泄露.死循.乱用指针.数组越界(数组开小了?).除以0错误.递归太深层(系统暴栈了)

  4. Nginx 配置指令的执行顺序(一)

    大多数 Nginx 新手都会频繁遇到这样一个困惑,那就是当同一个 location 配置块使用了多个 Nginx 模块的配置指令时,这些指令的执行顺序很可能会跟它们的书写顺序大相径庭.于是许多人选择了 ...

  5. CKeditor 集成 CKFinder

    之前照着网上的做,遇到了一些问题,经过多次实验修改最后算是成功了,下面进行详细讲解. 一.CKeditor的配置(附件中已有最新版CKeditor和CKFinder) 1.需要下载ckeditor, ...

  6. Script Browser &amp&semi; Script Analyzer 1&period;3更新发布

    感谢Windows PowerShell MVP Kirk Munro.Laurent Dardenne在过去三个星期内为我们提出的各种想法和建议.针对这些的建议,我们对Script Browser ...

  7. git撤销提交&lpar;commit&rpar;

    我们知道Git有三大区(工作区.暂存区.版本库)以及几个状态(untracked.unstaged.uncommited) 一.简介 Git 保存的不是文件的变化或者差异,而是一系列不同时刻的文件快照 ...

  8. SQL调优(SQL TUNING)之远程支持完成性能大幅优化

    前几天,一个朋友找到我,说一个SQL性能有问题,看看能不能优化,下面为过程: 雪豹 9:35:10 在吗 兰花岛主 15:07:39 忙忘了,有事儿? 雪豹 15:07:49 嗯 雪豹 15:07:5 ...

  9. SQL 字段查找

    select [name] from sysobjects where [id] in (select [id] from syscolumns where [name]='a1') SQL 2005 ...

  10. 创建一个简单的 MDM server&lpar;1&rpar;

    前提:已获得 APNS 证书 ,已完毕 MDM 配置描写叙述文件的制作.请參考< MDM 证书申请流程 >一文和<配置MDM Provisioning Profile>. 环境 ...