More simple and clean java code with comments


  • 0
    M
    public int read(char[] buf, int n) {
            char[] tmp = new char[4];
            int index = 0;//number of char you have read
            int curRead  = 4;   
            
            while(curRead  == 4) {
                curRead  = read4(tmp);
                // if n = 10, index = 8 and curRead = 4
                // you only need 2 more in tmp
                // if ......................curRead = 1
                // you can only read 1 more
                curRead  = Math.min(curRead , n - index);
                for(int i = 0; i < curRead; i++)
                    buf[index++] = tmp[i];
            }
            return index;
        }
    

Log in to reply
 

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