✡ leetcode 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) --------- java

时间:2022-05-06 08:57:05

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&lbrack;158&rsqb; Read N Characters Given Read4 II - Call multiple times

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

  2. &lbrack;leetcode&rsqb;158&period; 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&period; Read N Characters Given Read4 II - Call multiple times

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

  4. 158&period; 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. &lbrack;Locked&rsqb; Read N Characters Given Read4 &amp&semi; 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. &lbrack;LeetCode&rsqb; 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. &lbrack;Swift&rsqb;LeetCode158&period; 用Read4来读取N个字符II &dollar; 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. iOS 应用中有页面加载gif动画,从后台进入前台时就消失了

    解决办法: 在Appdelegate.m 里面有一个从后台进入前台所响应的方法,可以在该方法里post 一个通知,在加载动画里的页面接受通知,响应一定的方法即可 #pragma -mark 当程序进入 ...

  2. vijosP1210 盒子与球

    vijosP1210 盒子与球 链接:https://vijos.org/p/1210 [思路] Stirling+全排列. 因为第二类stirling所求是没有标明盒子顺序的方案数,所以最后需要乘一 ...

  3. &lpar;转&rpar; 如何命令查找linux系统版本!

    一.查看内核版本命令: 1) [root@SOR_SYS ~]# cat /proc/versionLinux version 2.6.18-238.el5 (mockbuild@x86-012.bu ...

  4. 浅谈JavaScript中的柯里化函数

    首先,不可避免的要引经据典啦,什么是柯里化函数呢(from baidu): 在计算机科学中,柯里化(Currying)是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返 ...

  5. run in thread

    def run_in_thread(runnable, is_daemon=True): server_thread = Thread(target=runnable) server_thread.s ...

  6. Java 8 新特性-菜鸟教程 &lpar;1&rpar; -Java 8 Lambda 表达式

    Lambda 表达式,也可称为闭包,它是推动 Java 8 发布的最重要新特性. Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中). 使用 Lambda 表达式可以使代码变的更加 ...

  7. Android友盟增量更新

    1.增量升级的原理 增量更新的原理就是将本地apk与服务器端最新版本比对,并得到差异包.比如现在的版本是1.1.4,大小是7.2M,新版本是1.1.5.大小是7.3M.我们发现两个版本只有0.1M的差 ...

  8. ChannelInitializer&colon; 每个channel都new ChannelHandle

    State management 1.业务状态管理-是否登录 A ChannelHandler often needs to store some stateful information. The ...

  9. ORACLE 参数设置绑定变量

    使用 CURSOR_SHARING 参数 EXACT  默认,不替换 SIMIAR 当替换不会影响到执行计划时,才会将字面量替换成绑定变量 FORCE 只要有可能,字面量会被替换为绑定变量

  10. 大数据开篇 MapReduce初步

    最近在学习大数据相关的东西,开这篇专题来记录一下学习过程.今天主要记录一下MapReduce执行流程解析 引子(我们需要解决一个简单的单词计数(WordCount)问题) 1000个单词 嘿嘿,100 ...