✡ leetcode 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) --------- java
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.
和157题一样,区别在于可以对一个文件多次使用read
那么区别就是在于如果之前调用过一次read,可能由于调用了read4的原因,会出现多读了几个字母(小于4个),那么设定一个变量即可。存储当前多读取的字母长度和字母。
1、需要注意的点:多次调用的时候,moreChars里的字母可能没有用完。
/* 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
*/
private int len = 0;
private char[] moreChars = new char[3];
public int read(char[] buf, int n) {
if (n < 1){
return 0;
}
int result = 0;
if (len != 0){
int count = Math.min(n, len);
for (int i = 0; i < count; i++){
buf[i] = moreChars[i];
result++;
}
for (int i = 0; i < (len - count); i++){
moreChars[i] = moreChars[count + i];
}
len -= count;
}
char[] chars = new char[4];
int times = (n - result) / 4;
for (int i = 0; i < times; i++){
int count = read4(chars);
for (int j = 0; j < count; j++){
buf[result + j] = chars[j];
}
result += count;
if (count < 4){
return result;
}
}
if (n == result){
return result;
}
int count = read4(chars);
for (int i = 0; i < Math.min(n - result, count); i++){
buf[result + i] = chars[i];
}
if (n - result < count){
len = count - n + result;
for (int i = 0; i < len; i++){
moreChars[i] = chars[n - result + i];
}
}
result += Math.min(n - result, count);
return result;
}
}
✡ leetcode 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) --------- java的更多相关文章
- leetcode[158] Read N Characters Given Read4 II - Call multiple times
想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...
- [leetcode]158. Read N Characters Given Read4 II - Call multiple times 用Read4读取N个字符2 - 调用多次
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- 158. 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 ...
- [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 ...
- 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 actu ...
- [Swift]LeetCode158. 用Read4来读取N个字符II $ Read N Characters Given Read4 II
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
随机推荐
- Logistic回归模型和Python实现
回归分析是研究变量之间定量关系的一种统计学方法,具有广泛的应用. Logistic回归模型 线性回归 先从线性回归模型开始,线性回归是最基本的回归模型,它使用线性函数描述两个变量之间的关系,将连续或离 ...
- Linux shell中运行命令后加上字符“&”的作用
上午登录服务器编译运行服务端程序的时候,学到了在命令后加上字符“&”后,退出shell,运行的命令可以继续运行.不解原因,并到网上搜索了以下,明白了点! 以下是搜索到的片段: & 放在 ...
- 【matlab】膨胀
clear all;close all; BW=zeros(9,10); BW(4:6,4:7) =1 imshow(BW) hold on SE=strel('square',3) BW2=imdi ...
- iOS下载使用系统字体
iOS下载使用系统字体 通用开发中一般使用系统默认的字体: 另外系统也提供了一些其他字体我们可以选择下载使用 1:在mac上打开 字体册 app 即可查找系统支持的字体,适用于ios上开发使用 从ma ...
- DbInitializer.cs初始化过程中context.entityName.Add()遇到的类型不匹配错误
用Asp.Net Core+EF Core建立一个测试项目过程中,使用DbInitializaer.cs进行数据库表的初始化工作,当项目测试运行时执行到context.实体名.Add()时,提示错误信 ...
- laravel 中 与前端的一些事2 之使用Gulp编译sass
下载所有依赖npm的packagist: 下载了前端laravel elixir编译所需要的全部工具: gulp编译scss文件: scss文件的默认存放位置: 输入命令gulp 开始编译scss文 ...
- 使用CSS3 BACKFACE-VISIBILITY属性制作翻转动画效果
摘要: 通过backface-visibility:hidden;属性,我们可以使一个元素在翻转之后消失,这是可以使用另一个元素放在它的背面,从而制作出一种元素翻转之后出现另一个元素的效果. ... ...
- Sphinx 的介绍和原理探索——不存储原始数据,原始数据来源于SQL,而生成索引放在内存或者磁盘中
摘自:http://blog.jobbole.com/101672/ What/Sphinx是什么 定义:Sphinx是一个全文检索引擎. 特性: 索引和性能优异 易于集成SQL和XML数据源,并可使 ...
- Icon资源详解[2]
本文分享&备忘最近了解到的icon资源在windows平台下相关的一部分知识.所有测试代码都尽可能的依赖win32 API实现.通过源码可以了解其结构,同时它们也是可复用的代码积累. ...
- PHP EMS: 开源 在线考试系统安装
PHPEMS: 在线考试系统调测记录 下载安装软件包 PE2014.RAR 环境要求:利用了RHEL 5.X的一个环境,系统要求的运行环境是PHP 5.2以上,MYSQL 5.0以上.看了一下光盘,发 ...