✡ 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 ...
随机推荐
- do while(false)实用技巧
今天看项目源码的时候发现有些地方用了do{} while(false)的用法,查了下发现这样确实有些优点,mark下. 1.最重要的优点,用在略微复杂的宏定义中. #define AB1 a; b; ...
- FACADE
1 意图:为子系统中的一组接口提供一个一直的界面,Facade模式定义了一个高层接口.这个接口使得这一子系统更加容易使用, 2 动机,便于不需要更多功能的人通过Facade 简化使用 3 适用性: . ...
- MySQL 死锁问题分析
转载: MySQL 死锁问题分析 线上某服务时不时报出如下异常(大约一天二十多次):"Deadlock found when trying to get lock;". Oh, M ...
- 调用天气预报webservice
<script src="jquery.js" type="text/javascript" charset="utf-8">& ...
- (36)老版和新版API调用
---------更新时间18:06 2016-09-18 星期日------- *前言 我用的是odoo8,但里面有相当多的api是以前版本,这时若我们自己开发的 插件采用新版本api,里面 ...
- WPF仿Word头部格式,涉及DEV RibbonControl,NarvbarControl,ContentPresenter,Navigation
时隔1个月,2015/06/17走进新的环境. 最近一个星期在学习仿Word菜单栏的WPF实现方式,废话不多说,先看一下效果. 打开界面后,默认选中[市场A],A对应的菜单栏,如上图, 选择[市场B] ...
- javascript思维导图
JavaScript 数组 JavaScript 函数基础 Javascript 运算符 JavaScript 流程控制 JavaScript 正则表达式 JavaScript 字符串函数 JavaS ...
- 第一个Java web项目:员工管理系统
要求: 做一个登陆页面,实现登录,用户名和密码都是admin,登录成功后,用session记录用户名,登录失败,请提示失败原因. 做一个简单的管理系统,实现注册,修改,查询,删除 员工的功能,注册内容 ...
- JavaWeb chapeter 5 Web应用程序状态管理
1. HTTP协议使用的是无状态连接,对容器而言,每一个请求都来自于一个新的客户. 2. html表单隐藏字段:对用户在网站上的访问进行会话跟踪.为服务器端程序提供预定义的输入.存储动态产生的页面上 ...
- Shannon entropy
Shannon entropy is one of the most important metrics in information theory. Entropy measures the unc ...