LeetCode 158: Read N Characters Given Read4 II

Posted keepshuatishuati

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 158: Read N Characters Given Read4 II相关的知识,希望对你有一定的参考价值。

/* 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
     */
    private int prevStart = 0;
    private int prevEnd = 0;
    private char[] buffered = new char[4];
    public int read(char[] buf, int n) {
        int currentStart = 0;
        while (currentStart < n) {
            if (prevStart == 0) {
                prevEnd = read4(buffered);
            }
            if (prevEnd == 0) {
                break;
            }
            while (currentStart < n && prevStart < prevEnd) {
                buf[currentStart++] = buffered[prevStart++];
            }
            if (prevStart >= prevEnd) {
                prevStart = 0;
            }
        }
        return currentStart;
    }
}

 

以上是关于LeetCode 158: Read N Characters Given Read4 II的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 158. Read N Characters Given Read4 II - Call multiple times

[leetcode]158. Read N Characters Given Read4 II - Call multiple times 用Read4读取N个字符2 - 调用多次

158. Read N Characters Given Read4 II - Call multiple times

java 158.读取N个字符给出Read4 II - 多次调用(1st).java

java 158.读取N个字符给出Read4 II - 多次调用(1st).java

java 158.读取N个字符给出Read4 II - 多次调用(1st).java