@@ -0,0 +1,3105 @@
+package com.freetymekiyan.algorithms.level.hard;%0A%0A/**%0A * The API: int read4(char *buf) reads 4 characters at a time from a file.%0A * %3Cp%3E%0A * The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters%0A * left in the file.%0A * %3Cp%3E%0A * By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.%0A * %3Cp%3E%0A * Note:%0A * The read function may be called multiple times.%0A * %3Cp%3E%0A * Company Tags: Bloomberg, Google, Facebook%0A * Tags: String%0A * Similar Problems: (E) Read N Characters Given Read4%0A */%0Apublic class ReadNCharactersGivenRead42 %7B%0A%0A    /**%0A     * The read4 API is defined in the parent class Reader4.%0A     * int read4(char%5B%5D buf);%0A     */%0A    public class Solution extends Reader4 %7B%0A%0A        private int bufferPointer = 0;%0A        private int bufferCounter = 0;%0A        private char%5B%5D buffer = new char%5B4%5D;%0A%0A        /**%0A         * Read from the file into an intermediate buffer.%0A         * Write from that buffer to the outside buffer.%0A         * If that buffer is used up, read from file again.%0A         * If that buffer still has characters, keep for the next time.%0A         * Return when we reach the end of file, or we reach n.%0A         *%0A         * @param buf Destination buffer%0A         * @param n   Maximum number of characters to read%0A         * @return The number of characters read%0A         */%0A        public int read(char%5B%5D buf, int n) %7B%0A            int readBytes = 0;%0A            while (readBytes %3C n) %7B%0A                if (bufferPointer == 0) %7B // Refill intermediate buffer if needed.%0A                    bufferCounter = read4(buffer);%0A                %7D%0A                if (bufferCounter == 0) %7B // End of file%0A                    break;%0A                %7D%0A                while (readBytes %3C n && bufferPointer %3C bufferCounter) %7B // Copy to outside buffer%0A                    buf%5BreadBytes++%5D = buffer%5BbufferPointer++%5D;%0A                %7D%0A                if (bufferPointer == bufferCounter) %7B // Outside buffer used up%0A                    bufferPointer = 0;%0A                %7D%0A            %7D%0A            return readBytes;%0A        %7D%0A    %7D%0A%0A    /**%0A     * What is the difference between call once and call multiple times?%0A     * When you call read4() which reads 4 bytes into your buffer you might read more than you need.%0A     * So you want to store those bytes.%0A     * And next time you call read will start from those stored bytes, then read more from the file.%0A     * %3Cp%3E%0A     * Example:%0A     * You have 4 chars %22a, b, c, d%22 in the file, and you want to call your function twice like this:%0A     * read(buf, 1); // should return 'a'%0A     * read(buf, 3); // should return 'b, c, d'%0A     * All the 4 chars will be consumed in the first call of read4().%0A     * So the tricky part of this question is:%0A     * how can you preserve the remaining 'b, c, d' to the second call.%0A     * https://discuss.leetcode.com/topic/36179/what-is-the-difference-between-call-once-and-call-multiple-times%0A     */%0A    private class Reader4 %7B%0A%0A        int read4(char%5B%5D buf) %7B%0A            return 0;%0A        %7D%0A    %7D%0A%7D%0A

add read n characters given read 4 2