157. Read N Characters Given Read4
题目:
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;
}
}
二刷:
- 先建立一个read4Buf来保存使用read4 api之后读取的数据。 并且我们建立一个boolean EOF来代表是否读到了文件末尾,即read4返回的值小于4。
- 接下来我们用一个while循环来读取
- 当read4返回的值小于4的时候,我们设置EOF = true,即在下一次循环跳出
- 接下来我们来判断每次究竟要读取多少个bytes, 这个bytesToRead = Math.min(n - bytesRead,read4Bytes), 就是还剩多少字符要读取,以及read4 api的返回值里较小的一个
- 我们根据这个bytesToRead将read4Buf里的值拷贝到buf里,并且增加bytesRead
- 最后循环结束后,我们返回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的更多相关文章
- [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 ...
- [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 ...
- 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
- ✡ 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 ...
- 【LeetCode】157. Read N Characters Given Read4
Difficulty: Easy More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...
- [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 ...
- [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 ...
- 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 ...
- LeetCode Read N Characters Given Read4 II - Call multiple times
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...
随机推荐
- android N刷机
1. 打开开发者选项2. 在开发者选项页面打开 oem解锁3. 运行 adb reboot bootloader4. 运行 fastboot flashing unlock5. 解压你下载的刷机包 6 ...
- Submine Text 3 代码自动换行
开启自动换行功能的步骤: 1.打开 Preferences -> Setting - User(设置 - 用户) 2.在再大的括号前添加如下内容即可: "word_wrap" ...
- zookeeper实现商品秒杀抢购
package com.test; import java.io.IOException; import java.util.List; import java.util.concurrent.Cyc ...
- Android logcat使用
Android logcat使用 1. Android日志说明 当Android系统运行的时候,会搜集所有的系统信息. logcat是Android系统的一个命令行工具,主要用来查看和过滤日志信息. ...
- SVN服务器从Windows迁移到Linux
gerui 2013.9.14 ge-rui@sohu.com 一.备份VisualSVN项目 1. 现在要使用Linux作为svn服务器,之前是在windows Server 2008上的,用的是V ...
- TreeMap 红黑树实现
TreeMap 是一个有序的key-value集合,它是通过 红黑树 实现的. TreeMap 继承于AbstractMap,所以它是一个Map,即一个key-value集合. TreeMap 实现了 ...
- (转)互联网协议入门 ------ HTTP(1)
作者:阮一峰 原文:http://www.ruanyifeng.com/blog/2012/06/internet_protocol_suite_part_ii.html 我们每天使用互联网,你是否想 ...
- python mysqldb连接数据库
今天无事想弄下python做个gui开发,最近发布的是python 3k,用到了数据库,通过搜索发现有一个mysqldb这样的控件,可以使用,就去官方看了下结果,没有2.6以上的版本 没办法就下了一个 ...
- oracle 行转列问题
select id, name, ),),)) "imp_value", ),), )) "click_value" from (SELECT a.id, a. ...
- uvision4 ide已停止工作
情景描述: 笔者安装了新系统WIN8.1,装上了MDKV4.72.MDK编译程序可以正常工作,可是只要当我“下载程序”或者“调试程序”的时候就提示“uvision4 ide已停止工作”,迫不得已只能关 ...