My Java solution


  • 1
    J
    public class Solution extends Reader4 {
    char[] buffer = new char[4];
    int bufferSize = 0;
    int offset = 0;
    
    public int read(char[] buf, int n) {
    	if (n == 0) {
    		return 0;
    	}
    	int count = 0;
    	boolean eof = false; //end of file reached if true;
        while ((!eof) && count < n) {
        	if (bufferSize <= 0) { //initiate a new read4 call.
        		bufferSize = read4(buffer);
        		offset = 0;
        		if (bufferSize < 4)
        			eof = true;
        	}
        	int readLen;
        	if (count + bufferSize <= n) {
        		readLen = bufferSize;
        	} else {
        		readLen = n - count;
        	}
        	System.arraycopy(buffer, offset, buf, count, readLen);
        	offset = (offset + readLen) % 4;
        	bufferSize = bufferSize - readLen;
        	count += readLen;
        }
        return count;
    } }

  • 1
    D

    I got this:
    ""
    [read(0),read(1)]
    Output:
    ["","\x00"]
    Expected:
    ["",""]


Log in to reply
 

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