本题真的非常简单,就是题目有点晦涩,其实很容易,大概思路就是有个额外的int[] temp
来存储read4(char[])
每次的结果即可,代码如下:
/**
* 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 Number of characters to read
* @return The number of actual characters read
*/
public int read(char[] buf, int n) {
int total = 0;
while (true) {
char[] tmp = new char[4];
int len = read4(tmp);
for (int i = 0; i < len; ++i) {
buf[total] = tmp[i];
++total;
}
if (total >= n || len < 4) {
break;
}
}
return Math.min(total, n);
}
}