158. Read N Characters Given Read4 Ii - Call Multiple Times

Given a file and assume that you can but read the file using a given method read4 , implement a method read to read n characters. Your method read may be called multiple times .

The API read4 reads four consecutive characters from file , and then writes those characters into the buffer array buf4 .

The return value is the number of actual characters read.

Annotation that read4() has its own file arrow, much like FILE *fp in C.

buf4[] is a destination, not a source. The results from read4 will be copied to buf4[].

Below is a loftier-level example of how read4 works:

File file("abcde"); // File is "abcde", initially file pointer (fp) points to 'a'

char[] buf4 = new char[iv]; // Create buffer with enough space to store characters

read4(buf4); // read4 returns 4. Now buf4 = "abcd", fp points to 'eastward'

read4(buf4); // read4 returns 1. Now buf4 = "east", fp points to end of file

read4(buf4); // read4 returns 0. Now buf4 = "", fp points to end of file

By using the read4 method, implement the method read that reads n characters from file and store it in the buffer array buf . Consider that yous cannot manipulate file direct.

The render value is the number of bodily characters read.

Parameters: char[] buf, int n

buf[] is a destination, non a source. You will demand to write the results to buf[].

  • Consider that you cannot dispense the file directly. The file is only accessible for read4 simply not for read .

  • The read function may be called multiple times .

  • Please remember to RESET your class variables declared in Solution, equally static/class variables are persisted beyond multiple test cases. Please see here for more details.

  • Y'all may assume the destination buffer array, buf , is guaranteed to have plenty space for storing n characters.

  • It is guaranteed that in a given test instance the aforementioned buffer buf is called by read .

Input: file = "abc", queries = [ane,ii,1]

Explanation: The exam case represents the post-obit scenario:

sol.read(buf, 1); // After calling your read method, buf should incorporate "a". Nosotros read a total of ane character from the file, so render 1.

sol.read(buf, two); // At present buf should contain "bc". We read a total of 2 characters from the file, so render 2.

sol.read(buf, ane); // We have reached the end of file, no more characters tin be read. And then render 0.

Assume buf is allocated and guaranteed to have enough space for storing all characters from the file.

Input: file = "abc", queries = [4,one]

Caption: The examination instance represents the following scenario:

sol.read(buf, 4); // After calling your read method, buf should comprise "abc". We read a total of 3 characters from the file, so return 3.

sol.read(buf, 1); // We have reached the cease of file, no more than characters can be read. And then render 0.

  • file consist of English letters and digits.

  • 1 <= queries.length <= 10

Solution

* @param buf Destination buffer

* @param due north Number of characters to read

* @return The number of actual characters read

int read(char *buf, int n) {

while (cache_cnt > 0 && ind < northward) {

buf[ind] = cache[cache_ind];

cache_cnt = read4(enshroud);

villanuevaingents.blogspot.com

Source: https://zhenchaogan.gitbook.io/leetcode-solution/leetcode-158-read-n-characters-given-read4-ii-call-multiple-times

0 Response to "158. Read N Characters Given Read4 Ii - Call Multiple Times"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel