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 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- LeetCode Read N Characters Given Read4 II - Call multiple times
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...
- 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- [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
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...
- [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
Problem: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is ...
随机推荐
- 微信小程序 教程及示例
作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有,转载请联系作者获得授权.微信小程序正式公测, ...
- QT笔记
1.菜单栏上的弹出窗口 void MainWindow::on_new_action_triggered() { MyDialog myDialog;//MyDialog是一个ui m ...
- SQL Server2008 MERGE指令用法
参考资料: 百度百科-MERGE
- 通过mongodb客户端samus代码研究解决查询慢问题
最近有项目需要用到mongodb,于是在网上下载了mongodb的源码,根据示例写了测试代码,但发现一个非常奇怪的问题:插入记录的速度比获取数据的速度还要快,而且最重要的问题是获取数据的速度无法让人接 ...
- ubuntu系统下使用锐捷上网的方法——特快
1.点击右上方的有线连接: 2.点击编辑连接: 3.选择802.x的安全性分页: 4.勾选下面的单选框: 5.输入登陆账号(通常就是学号),密码(通常是身份证后六位),如果出现了一个“解锁密码环”的对 ...
- CF459A Pashmak and Garden (水
Pashmak and Garden Codeforces Round #261 (Div. 2) A. Pashmak and Garden time limit per test 1 second ...
- jQuery 学习之路(2):选择器与过滤器
一.基本选择器 标签选择器: $('button') ID选择器: $('#id1') 类选择器: $('.class1') 多重选择器: $('#id1,.class1,button') 全体选择器 ...
- jQuery.validator 详解二
前言:上一篇详细的介绍了jQuery.validator( 版本v1.13.0 )的验证规则,这一篇重点讲述它的源码结构,及如何来对元素进行验证,错误消息提示的内部实现 一.插件结构(组织方式) 在讲 ...
- Maven初级学习(二)Maven使用入门
序,学习配置pom.xml,利用maven生成eclipes项目. 一.编写POM POM Project Obejct Model,项目对象模型. 编写pom.xml,新建文件夹hello-worl ...
- Linux中服务器软件为什么需要编译安装
为什么服务器软件需要编译安装?一个流传很广的说法是编译安装性能更好,其实这是个谣言. 服务器CPU事实已经被Intel垄断了,就那么几种型号,编来编去生成的机器码是一样的.Intel宣传自己的编译工具 ...