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