Read N Characters Given Read4 II – Call multiple times

用一个read4实现readn, 而且readn还要能多次调用. 这个题主要难在read4一次读4个后,要和readn的n比较. 我直接用了queue做比较. 然后注意一定, 当读的超的时候, 要停止不读了.

/**
 * 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
     */
    Queue<Character> q = new LinkedList<>();
    public int read(char[] buf, int n) {
        int t;
        do{
            char[] c = new char[4];
            t = read4(c);
            for(int i = 0; i < t; i++) {
                q.add(c[i]);
            }
        }while(n > q.size() && t != 0);
        int cur = 0;
        int size = q.size();
        for(int i = 0; i < Math.min(n, size); i++) {
            buf[i] = q.poll();
            cur++;
        }
        return cur;
    }
}