Leetcode-Read N Characters Given Read4 II

时间:2021-08-04 06:15:50

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.

Analysis:

Since the function can be called multiple times, we need to record the left over content in the buffer of read4, and put them in some place. For the next call, we need to read content from the carry over buffer first.

Solution:

 /* 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
*/ public char[] carry;
public int index; public int read(char[] buf, int n) {
int left = n; //Put carry over into buf, and destroy the carry over array!
if (carry!=null){
while (left>0 && index<carry.length){
buf[n-left]=carry[index];
left--;
index++;
}
if (index>=carry.length){
carry=null;
index=-1;
}
} char[] tempBuf = new char[4];
while (left>0){
int num = read4(tempBuf);
//if the read number is larger then what we need, then we just put the left number of chars into buf.
//And put the rest chars into carry array.
if (num>left){
carry = new char[num-left];
for (int i=left;i<num;i++)
carry[i-left] = tempBuf[i];
index = 0;
} int end = Math.min(num,left);
for (int i=0;i<end;i++){
buf[n-left] = tempBuf[i];
left--;
} //If reach EOF.
if (left>0 && num<4) break;
} return n-left; }
}

Leetcode-Read N Characters Given Read4 II的更多相关文章

  1. &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 ...

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

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

  3. &lbrack;LeetCode&rsqb; Read N Characters Given Read4 I &amp&semi; II

    Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...

  4. &lbrack;LeetCode&rsqb; Read N Characters Given Read4 用Read4来读取N个字符

    The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...

  5. LeetCode Read N Characters Given Read4

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...

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

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

  8. ✡ leetcode 158&period; 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 actu ...

  9. leetcode&lbrack;158&rsqb; Read N Characters Given Read4 II - Call multiple times

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

  10. &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 ...

随机推荐

  1. Centos7下Etcd集群搭建

    一.简介 "A highly-available key value store for shared configuration and service discovery." ...

  2. 【NOIP2009 T3】 最佳贸易 (双向SPFA)

    C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道 ...

  3. jquery&comma;extjs中的extend用法小结

    在jquery中,extend其实在做插件时还是用的比较多的,今天同时小结jquery和ext js中 的extend用法,先来看jquery中的. 1)  extend(dest,src1,src2 ...

  4. 《A First Course in Probability》-chape4-离散型随机变量-几种典型分布列

    超几何分布: 超几何分布基于这样一个模型,一个坛子中有N个球,其中m个白球,N-m个黑球,从中随机取n(不放回),令X表示取出来的白球数,那么: 我们称随机变量X满足参数为(n,m,M)的超几何分布. ...

  5. SRM 408&lpar;1-250pt&comma; 1-500pt&rpar;

    DIV1 250pt 题意:每天晚上需要点蜡烛,且每晚蜡烛燃烧1cm,第i天晚上需要点i根蜡烛.第一天白天的时候,拥有一些蜡烛,用vector<int>can表示他们的长度,问最多能烧几个 ...

  6. poj3258

    题目翻译 二分法(其实两个单词的意思分别是河,跳格子游戏,至于为啥翻译成二分法- -只能说英语博大精深啊) 奶牛每年举办一场有特色的跳格子游戏(很明显题目翻译错误)涉及到在河里从一块岩石跳到另一块岩石 ...

  7. MAC使用GITHUB

    作者:珊姗是个小太阳链接:https://www.zhihu.com/question/20070065/answer/79557687来源:知乎著作权归作者所有,转载请联系作者获得授权. #什么是 ...

  8. session与cookie-----2017-05-08

    会话控制:目的是记录不同用户身份. 1.session:有实效性 特点: (1)存在在服务器 (2)每个用户都会存一份 (3)可以存储任意类型的数据 优点:安全性高 缺点:服务器压力过大 2.cook ...

  9. java基础07 多线程

    在学习操作系统时,我们会学习进程和线程,那么进程和线程又是什么东西呢? 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位. 线程(thread) ...

  10. Java线程调度—休眠

    线程休眠的方法是Thread.sleep(long millis) 和Thread.sleep(long millis, int nanos) ,均为静态方法,那调用sleep休眠的哪个线程呢?简单说 ...