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. PHP-----循环结构

    for循环语句 打印金字塔 完整的金字塔 //打印金字塔 $n=25; for($i=1;$i<=$n;$i++){ //空格循环 for($k=1;$k<=$n-$i;$k++){ ec ...

  2. SQL语言增加、修改、删除数据的语法

    增加 insert into 表名(字段1,字段2) values ('字段1的值','字段2的值'); 修改 update 表名 set 字段1='赋予字段1的新值',字段2='赋予字段2的新值' ...

  3. Delphi 使用之dll文件生成与调用

    DLL是Dynamic-Link Libraries(动态链接库)的缩写,库里面是一些可执行的模块以及资源(如位图.图标等).可以认为DLL和EXE基本上是一回事,只是DLL不能直接执行,而必须由应用 ...

  4. C++学习笔记一 —— 两个类文件互相引用的处理情况

    先记录一些零碎的知识点: 1. 一个类可以被声明多次,但只能定义一次,也就是可以 class B;  class B;  class B; ……;  class B {……};  这样子. 2. 一个 ...

  5. PHP 小方法之 写日志方法

    if(! function_exists ('write_log') ) { function write_log($data, $name='debug', $date=null){ if (is_ ...

  6. static代码块与{}代码块的比较

    第一个例子: public class StaticDemo { { System.out.println("{} 代码块"); } static{ System.out.prin ...

  7. Shell重定向文件描述符

    #!/bin/bash      最近在看shell,各种困惑,不过解决困惑的感觉还是很不错的.废话少说,linux中使用文件描述符来标识每个文件对象.文件描述符为一个非负整数,可以唯一标识会话中打开 ...

  8. 动态规划(一)——最长公共子序列和最长公共子串

    注: 最长公共子序列采用动态规划解决,由于子问题重叠,故采用数组缓存结果,保存最佳取值方向.输出结果时,则自顶向下建立二叉树,自底向上输出,则这过程中没有分叉路,结果唯一. 最长公共子串采用参考串方式 ...

  9. 自己封装的一个java图片验证码

    验证码生成器: package com.lz.Tools; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; ...

  10. c#网络通信框架networkcomms内核解析 序言

    NetworkComms网络通信框架序言 networkcomms是我遇到的写的最优美的代码,很喜欢,推荐给大家:) 基于networkcomms2.3.1开源版本( gplv3)协议,写了一些文章, ...