LeetCode 158: Read N Characters Given Read4 II
阿新 • • 發佈:2017-09-05
style pub fine start int col maximum nbsp leetcode
/* The read4 API is defined in the parent class Reader4. int read4(char[] buf); */ public class Solution extends Reader4 { /** * @param buf Destination buffer * @param n Maximum number of characters to read * @return The number of characters read */ private int prevStart = 0;private int prevEnd = 0; private char[] buffered = new char[4]; public int read(char[] buf, int n) { int currentStart = 0; while (currentStart < n) { if (prevStart == 0) { prevEnd = read4(buffered); } if (prevEnd == 0) {break; } while (currentStart < n && prevStart < prevEnd) { buf[currentStart++] = buffered[prevStart++]; } if (prevStart >= prevEnd) { prevStart = 0; } } return currentStart; } }
LeetCode 158: Read N Characters Given Read4 II