> For the complete documentation index, see [llms.txt](https://liuyang89116.gitbook.io/my-leetcode-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuyang89116.gitbook.io/my-leetcode-book/post_chapter_3_array/problem_157_read_n_characters_given_read4.md).

# Problem 157: Read N Characters Given Read4

> <https://leetcode.com/problems/read-n-characters-given-read4/>

![](/files/-LpvA-1kPHD1J5w9z-Ic)

## 思路

* read4() 每次读取的元素是 4 个。如果 n 可以被 4 整除，那么正好读完；如果不能被 4 整除，那么就需要在 n 的时候截止。

```java
/* 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 int read(char[] buf, int n) {
        char[] buf4 = new char[4];
        int count = 0;
        while (true) {
            int size = read4(buf4);
            for (int i = 0; i < size && count < n; i++) {
                buf[count++] = buf4[i];
            }

            if (size == 0 || count == n) {
                return count;
            }
        }
    }
}
```
