题目:

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. 20160503-spring入门2

    使用Spring需要的jar 到http://www.springsource.org/download下载spring,然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下 dist\sp ...

  2. Swift 2.0基本语法

    内容包括:01变量&常量 02分支 03循环 04字符串 05数组 06字典 07函数 01变量&常量 //: Playground - noun: a place where peo ...

  3. Objective-C 【多态】

    ------------------------------------------- 多态的概念.实现以及注意事项 程序中的多态:不同的对象    以自己的方式去    响应   相同方法名(父类同 ...

  4. Codevs 1218 疫情控制 2012年NOIP全国联赛提高组

    1218 疫情控制 2012年NOIP全国联赛提高组 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description H 国有 n 个城市,这 ...

  5. c&c++函数的参数和返回值的传递终结版

    c++函数的参数和返回值的传递方式有三种:值传递.指针传递和引用传递. 在这之前先看几个例子: 一, int a=10; int b=a; b+=10; 此时b是a的一个拷贝,改变b的值,a并不会受到 ...

  6. 程序员面试题精选100题(16)-O(logn)求Fibonacci数列[算法]

    作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:定义Fibonacci数列如下: /  0                      n=0 f(n)=      ...

  7. ubuntu12.10可用更新源

    ubutnu12.10自带的更新源已经失效,国内各大服务器的更新源,无论是网易.搜狐还是教育网的因此也失效了.附件是ubuntu12.10目前的可用更新源. 将地址中的“us.archive”换成“o ...

  8. java集合——进度1

    集合类的由来:    对象用于封装特有数据,对象多了需要存储,如果对象的个数不确定.    就使用集合容器进行存储.    集合特点:1,用于存储对象的容器.2,集合的长度是可变的.3,集合中不可以存 ...

  9. 为什么要有binary-to-text encoding?

    在wikipedia上看MIME的介绍的时候,有一节是关于Content-Transfer-Encoding的,里面提到了binary-to-text encoding,我就想,既然计算机中的信息使用 ...

  10. PHP常见算法-面试篇(2)

    1.顺序查找 思路分析: 从数组的第一个元素开始一个一个向下查找,如果有和目标一致的元素,查找成功:如果到最后一个元素仍没有目标元素,则查找失败. 代码实现: <?php function se ...