158. Read N Characters Given Read4 II - Call multiple times Given buf = "abc" read("abc", 1) // returns "a" read("abc", 2); // returns "bc" read("abc", 1); // returns "" Example 2: Given buf = "abc" read("abc", 4) // returns "abc" read("abc", 1); // returns "" private int buffPtr = 0; private int buffCnt = 0; private char[] buff = new char[4]; public int read(char[] buf, int n) { int ptr = 0; while (ptr < n) { if (buffPtr == 0) { buffCnt = read4(buff); } if (buffCnt == 0) break; while (ptr < n && buffPtr < buffCnt) { buf[ptr++] = buff[buffPtr++]; } if (buffPtr >= buffCnt){ buffPtr = 0; } } return ptr; } I used buffer pointer (buffPtr) and buffer Counter (buffCnt) to store the data received in previous calls. In the while loop, if buffPtr reaches current buffCnt, it will be set as zero to be ready to read new data.
158. Read N Characters Given Read4 II - Call multiple times
原文:https://www.cnblogs.com/tobeabetterpig/p/9490943.html