Java 1ms solution, One pointer, No End-of-file flag, Explained.


  • 0
    T

    // use pointer start to indicate the current filling position in global char array read[].
    // use a char array buffer[] of length 4 as an intermediate container for new characters just read, and later output these character to global char array read[]

        public int read(char[] read, int n) {
            char[] buffer = new char[4];
            int start = 0;
            while (start <= n - 1) {
                int count = read4(buffer);
                for (int i = 0; i < count && start <= n - 1; i++) {
                    read[start++] = buffer[i];
                }
                if (count < 4) {
                    break;
                }
            }
            return start;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.