Given a file and assume that you can only read the file using a given method read4
, implement a method to read n characters.
Method read4:
The API read4
reads 4 consecutive characters from the file, then writes those characters into the buffer array buf
.
The return value is the number of actual characters read.
Note that read4()
has its own file pointer, much like FILE *fp
in C.
Definition of read4:
Parameter: char[] buf Returns: int Note: buf[] is destination not source, the results from read4 will be copied to buf[]
Below is a high level example of how read4
works:
File file("abcdefghijk"); // File is "abcdefghijk", initially file pointer (fp) points to 'a'
char[] buf = new char[4]; // Create buffer with enough space to store characters
read4(buf); // read4 returns 4. Now buf = "abcd", fp points to 'e'
read4(buf); // read4 returns 4. Now buf = "efgh", fp points to 'i'
read4(buf); // read4 returns 3. Now buf = "ijk", fp points to end of file
思路:就是每次read4只能读一次,然后根据count决定是否加入buf,注意考虑读出来的是0,则直接break,不读了。
/**
* The read4 API is defined in the parent class Reader4.
* int read4(char[] buf4);
*/
public class Solution extends Reader4 {
/**
* @param buf Destination buffer
* @param n Number of characters to read
* @return The number of actual characters read
*/
public int read(char[] buf, int n) {
int totalcount = 0;
int index = 0;
while(totalcount < n) {
char[] buf4 = new char[4];
int curcount = read4(buf4);
// 不要超出n, 剩下的就是n - totalcount;
curcount = Math.min(curcount, n - totalcount);
for(int i = 0; i < curcount; i++) {
buf[index++] = buf4[i];
totalcount++;
}
if(curcount < 4) {
break;
}
}
return totalcount;
}
}