本题真的非常简单,就是题目有点晦涩,其实很容易,大概思路就是有个额外的int[] temp来存储read4(char[])每次的结果即可,代码如下:

    1. /**
    2. * The read4 API is defined in the parent class Reader4.
    3. * int read4(char[] buf);
    4. */
    5. public class Solution extends Reader4 {
    6. /**
    7. * @param buf Destination buffer
    8. * @param n Number of characters to read
    9. * @return The number of actual characters read
    10. */
    11. public int read(char[] buf, int n) {
    12. int total = 0;
    13. while (true) {
    14. char[] tmp = new char[4];
    15. int len = read4(tmp);
    16. for (int i = 0; i < len; ++i) {
    17. buf[total] = tmp[i];
    18. ++total;
    19. }
    20. if (total >= n || len < 4) {
    21. break;
    22. }
    23. }
    24. return Math.min(total, n);
    25. }
    26. }