Take a lot of time to come up with a readable java code

  • 0

    Basic principles I tried to do in this code

    1. Read from readoutbuffer first before any "read4" called to make sure the lasting char in the readoutbuffer could be read
    2. If this is the first time you "read()" you could skip the reading procedure
    3. index for readoutbuffer and readbits from "read4" should be refresh at last of while loop

    public class Solution extends Reader4 {

    char[] readoutbuff = new char[4];
    int ind = 0;
    int readbits = 0;
    public int read(char[] buf, int n) {
        if(n == 0) return 0;
        int buffer_index = 0;
        while(buffer_index < n){
            while(ind < readbits  && buffer_index < n){
                buf[buffer_index++] = readoutbuff[ind++];
                if(buffer_index == n) return n;
            readbits = read4(readoutbuff);
            ind = 0;
            if(readbits == 0) return buffer_index;
        return n;


Log in to reply

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