Leetcode 157,158, Read N Characters Given Read4
1592 ワード
public int read(char[] buf, int n) {
char[] buffer = new char[4];
int total = 0;
boolean eof = false; //end of file
while(!eof && total < n){
int count = read4(buffer);
if(count < 4){
eof = true;
}
// get the actual count
count = Math.min(count, n - total);
for(int i = 0; i< count; i++){
buf[total++] = buffer[i];
}
}
return total;
}
参照先:http://www.cnblogs.com/anne-vista/p/4865612.html
private int offSet = 0;
private int remaining = 0;
private boolean eof = false;
private char[] buffer = new char[4];
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
public int read(char[] buf, int n) {
int total = 0;
while (total < n && (remaining != 0 || !eof)) {
int count = 0;
if (remaining != 0) {
count = remaining;
} else {
offSet = 0;
count = read4(buffer);
if (count != 4) {
eof = true;
}
}
int length = Math.min(n - total, count);
for (int i= offSet; i
参照:https://leetcode.com/discuss/19581/clean-accepted-java-solution