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的更多相关文章

  1. leetcode[158] Read N Characters Given Read4 II - Call multiple times

    想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...

  2. [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 ...

  3. 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...

  4. 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 ...

  5. [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 ...

  6. [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 ...

  7. LeetCode Read N Characters Given Read4 II - Call multiple times

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. shell awk入门

    本文参考自 http://www.cnblogs.com/zhuyp1015/archive/2012/07/11/2586985.html awk:好用的数据处理工具 awk 也是一个非常棒的数据处 ...

  2. java练习题:解一元二次方程、判断闰年、判断标准身材、三个数取最大值

    1.解一元二次方程 注:求根公式为(-b+根号德尔塔)/2a,(-b-根号德尔塔)/2a Scanner sc=new Scanner(System.in); System.out.println(& ...

  3. 在centos6.5中安装github的客户端git

    简介 git是一个分布式版本控制软件,我采用的采用的安装方式是源码安装 1.安装编译git时需要的包 # yum install curl-devel expat-devel gettext-deve ...

  4. 001_kafka起步

    一.简介 Kafka is a distributed, partitioned, replicated commit log service. It provides the functionali ...

  5. ImportError: No module named MySQLdb

    ImportError: No module named MySQLdb 该错误是源于我们没有安装Python连接MySQL所需的MySQLdb库而引起. python3.5下的解决方法ubuntu系 ...

  6. vs---错误收集并自己解决后归纳

    1.C++编译时,出现这样的错误 d:\program files\microsoft visual studio\vc98\include\stdio.h(36) : error C2143: sy ...

  7. WPF CAL 计算器

    界面最终结果: 下载地址:https://skydrive.live.com/redir?resid=25C3908AA2038BDB!148&authkey=!ADR71XdB04LipYE

  8. shopex最新版前台一处想不到的SQL注入漏洞

    shopex代码核心的地方都做了加密处理,找漏洞就需要一点想象空间了,比如这个SQL注入… 存在于用户注册(想不到的位置吧?)   /core/shop/controller/ctl.passport ...

  9. hessian 协议

    什么是Hessian协议呢? 目前,Web服务技术是解决异构平台系统的集成及互操作问题的主流技术. 它所基于的XML已经是Internet上交换数据的实际标准,基于通用的进程间通信协议和网络传输协议屏 ...

  10. iOS开发网络篇—搭建本地服务器

    iOS开发网络篇—搭建本地服务器 一.简单说明 说明:提前下载好相关软件,且安装目录最好安装在全英文路径下.如果路径有中文名,那么可能会出现一些莫名其妙的问题. 提示:提前准备好的软件 apache- ...