java 157.阅读N个字符给出Read4(1st).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 157.阅读N个字符给出Read4(1st).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[] buffer = new char[4];
        boolean flag = true;
        int curSize = 0;
        int i = 0;
        while(i < n && (curSize > 0 || flag)) {
            curSize = read4(buffer);
            flag = false;
            int j = 0;
            while(j < curSize && i < n) {
                buf[i++] = buffer[j++];
            }
        }
        return i;
    }
}
/* 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 ptr = 0;
        while (ptr < n) {
            int remaining = read4(buf4);
            if (remaining == 0) break;
            for (int i = 0; i < remaining && ptr < n; i++) {
                buf[ptr++] = buf4[i];
            }
        }
        return ptr;
    }
}

以上是关于java 157.阅读N个字符给出Read4(1st).java的主要内容,如果未能解决你的问题,请参考以下文章

java 157.阅读N个字符给出Read4(1st).java

java 157.阅读N个字符给出Read4(1st).java

java 157.阅读N个字符给出Read4(1st).java

java 157.阅读N个字符给出Read4(1st).java

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

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