|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
4 |
| - * The API: int read4(char *buf) reads 4 characters at a time from a file. |
| 4 | + 158. Read N Characters Given Read4 II - Call multiple times |
| 5 | + The API: int read4(char *buf) reads 4 characters at a time from a file. |
5 | 6 |
|
6 | 7 | The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
|
7 | 8 |
|
8 | 9 | By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
|
9 | 10 |
|
10 | 11 | Note:
|
11 | 12 | The read function may be called multiple times.
|
| 13 | +
|
| 14 | + Example 1: |
| 15 | +
|
| 16 | + Given buf = "abc" |
| 17 | + read("abc", 1) // returns "a" |
| 18 | + read("abc", 2); // returns "bc" |
| 19 | + read("abc", 1); // returns "" |
| 20 | +
|
| 21 | + Example 2: |
| 22 | +
|
| 23 | + Given buf = "abc" |
| 24 | + read("abc", 4) // returns "abc" |
| 25 | + read("abc", 1); // returns "" |
12 | 26 | */
|
13 | 27 | public class _158 {
|
14 | 28 |
|
15 |
| - /** |
16 |
| - * @param buf Destination buffer |
17 |
| - * @param n Maximum number of characters to read |
18 |
| - * @return The number of characters read |
19 |
| - */ |
20 |
| - private int buffPtr = 0; |
21 |
| - private int buffCnt = 0; |
22 |
| - private char[] buff = new char[4]; |
23 |
| - |
24 |
| - public int read(char[] buf, int n) { |
25 |
| - int ptr = 0; |
26 |
| - while (ptr < n) { |
27 |
| - if (buffPtr == 0) { |
28 |
| - buffCnt = read4(buff); |
29 |
| - } |
30 |
| - if (buffCnt == 0) { |
31 |
| - break; |
32 |
| - } |
33 |
| - while (ptr < n && buffPtr < buffCnt) { |
34 |
| - buf[ptr++] = buff[buffPtr++]; |
35 |
| - } |
36 |
| - if (buffPtr >= buffCnt) { |
37 |
| - buffPtr = 0; |
38 |
| - } |
39 |
| - } |
40 |
| - return ptr; |
41 |
| - } |
42 |
| - |
43 |
| - //This is a fake method to make IDE happy. |
44 |
| - private int read4(char[] buff) { |
45 |
| - return 1; |
46 |
| - } |
47 |
| - |
| 29 | + public static class Solution1 { |
| 30 | + /** |
| 31 | + * @param buf Destination buffer |
| 32 | + * @param n Maximum number of characters to read |
| 33 | + * @return The number of characters read |
| 34 | + */ |
| 35 | + private int buffPtr = 0; |
| 36 | + private int buffCnt = 0; |
| 37 | + private char[] buff = new char[4]; |
| 38 | + |
| 39 | + public int read(char[] buf, int n) { |
| 40 | + int ptr = 0; |
| 41 | + while (ptr < n) { |
| 42 | + if (buffPtr == 0) { |
| 43 | + buffCnt = read4(buff); |
| 44 | + } |
| 45 | + if (buffCnt == 0) { |
| 46 | + break; |
| 47 | + } |
| 48 | + while (ptr < n && buffPtr < buffCnt) { |
| 49 | + buf[ptr++] = buff[buffPtr++]; |
| 50 | + } |
| 51 | + if (buffPtr >= buffCnt) { |
| 52 | + buffPtr = 0; |
| 53 | + } |
| 54 | + } |
| 55 | + return ptr; |
| 56 | + } |
| 57 | + |
| 58 | + //This is a fake method to make IDE happy. |
| 59 | + private int read4(char[] buff) { |
| 60 | + return 1; |
| 61 | + } |
| 62 | + } |
48 | 63 | }
|
0 commit comments