|
| 1 | +--- |
| 2 | +id: read-n-characters-given |
| 3 | +title: Read N characters Given |
| 4 | +sidebar_label: 0157-Read N Characters Given |
| 5 | +tags: |
| 6 | + - Leet code |
| 7 | +description: "Solution to leetocde 157" |
| 8 | +--- |
| 9 | + |
| 10 | +### Problem Description |
| 11 | + |
| 12 | +Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters. |
| 13 | + |
| 14 | +### Examples |
| 15 | + |
| 16 | +Example 1: |
| 17 | + |
| 18 | +``` |
| 19 | +Input: file = "abc", n = 4 |
| 20 | +Output: 3 |
| 21 | +Explanation: After calling your read method, buf should contain "abc". We read a total of 3 characters from the file, so return 3. Note that "abc" is the file's content, not buf. buf is the destination buffer that you will have to write the results to. |
| 22 | +``` |
| 23 | + |
| 24 | +Example 2: |
| 25 | + |
| 26 | +``` |
| 27 | +Input: file = "abcde", n = 5 |
| 28 | +Output: 5 |
| 29 | +Explanation: After calling your read method, buf should contain "abcde". We read a total of 5 characters from the file, so return 5. |
| 30 | +``` |
| 31 | + |
| 32 | +Example 3: |
| 33 | + |
| 34 | +``` |
| 35 | +Input: file = "abcdABCD1234", n = 12 |
| 36 | +Output: 12 |
| 37 | +Explanation: After calling your read method, buf should contain "abcdABCD1234". We read a total of 12 characters from the file, so return 12. |
| 38 | +``` |
| 39 | + |
| 40 | +Example 4: |
| 41 | + |
| 42 | +``` |
| 43 | +Input: file = "leetcode", n = 5 |
| 44 | +Output: 5 |
| 45 | +Explanation: After calling your read method, buf should contain "leetc". We read a total of 5 characters from the file, so return 5. |
| 46 | +``` |
| 47 | + |
| 48 | +### Note: |
| 49 | + |
| 50 | +- Consider that you cannot manipulate the file directly, the file is only accesible for read4 but not for read. |
| 51 | +- The read function will only be called once for each test case. |
| 52 | +- You may assume the destination buffer array, buf, is guaranteed to have enough space for storing n characters. |
| 53 | + |
| 54 | +### Algorithm |
| 55 | + |
| 56 | +1. **Initialization**: |
| 57 | + |
| 58 | + - `buf4`: A temporary buffer that stores the characters read by `read4`. |
| 59 | + - `i4`: Index for iterating over `buf4`. |
| 60 | + - `n4`: Number of characters actually read by `read4`. |
| 61 | + - `i`: Index for iterating over the destination buffer `buf`. |
| 62 | + |
| 63 | +2. **Reading Characters**: |
| 64 | + |
| 65 | + - The loop continues until `i` (number of characters written to `buf`) reaches `n` (the requested number of characters). |
| 66 | + - If all characters in `buf4` are consumed (`i4 == n4`), `read4` is called again to refill `buf4` and reset `i4`. |
| 67 | + - If `read4` returns 0 (`n4 == 0`), it means the end of the file has been reached, and the function returns the number of characters read so far. |
| 68 | + - Characters from `buf4` are copied to `buf` one by one, and both indices (`i` and `i4`) are incremented accordingly. |
| 69 | + |
| 70 | +3. **Return**: |
| 71 | + - The function returns the total number of characters read, which is the value of `i`. |
| 72 | + |
| 73 | +### Code Implementation |
| 74 | + |
| 75 | +### C++ |
| 76 | + |
| 77 | +```cpp |
| 78 | + |
| 79 | +class Solution { |
| 80 | + public: |
| 81 | + /** |
| 82 | + * @param buf Destination buffer |
| 83 | + * @param n Number of characters to read |
| 84 | + * @return The number of actual characters read |
| 85 | + */ |
| 86 | + int read(char* buf, int n) { |
| 87 | + char* buf4 = new char[4]; |
| 88 | + int i4 = 0; // buf4's index |
| 89 | + int n4 = 0; // buf4's size |
| 90 | + int i = 0; // buf's index |
| 91 | + |
| 92 | + while (i < n) { |
| 93 | + if (i4 == n4) { // All the characters in the buf4 are consumed. |
| 94 | + i4 = 0; // Reset the buf4's index. |
| 95 | + n4 = read4(buf4); // Read <= 4 characters from the file to the buf4. |
| 96 | + if (n4 == 0) // Reach the EOF. |
| 97 | + return i; |
| 98 | + } |
| 99 | + buf[i++] = buf4[i4++]; |
| 100 | + } |
| 101 | + |
| 102 | + return i; |
| 103 | + } |
| 104 | +}; |
| 105 | +``` |
| 106 | +
|
| 107 | +### Python |
| 108 | +
|
| 109 | +```python |
| 110 | +class Solution: |
| 111 | + def __init__(self): |
| 112 | + self.buf4 = [''] * 4 # Temporary buffer to store characters read by read4 |
| 113 | + self.i4 = 0 # Index for iterating over buf4 |
| 114 | + self.n4 = 0 # Number of characters actually read by read4 |
| 115 | +
|
| 116 | + def read4(self, buf4): |
| 117 | + # This method is provided in the parent class or environment. |
| 118 | + pass |
| 119 | +
|
| 120 | + def read(self, buf, n): |
| 121 | + i = 0 # Index for the destination buffer buf |
| 122 | +
|
| 123 | + while i < n: |
| 124 | + if self.i4 == self.n4: |
| 125 | + self.n4 = self.read4(self.buf4) |
| 126 | + self.i4 = 0 |
| 127 | + if self.n4 == 0: |
| 128 | + break |
| 129 | +
|
| 130 | + while i < n and self.i4 < self.n4: |
| 131 | + buf[i] = self.buf4[self.i4] |
| 132 | + i += 1 |
| 133 | + self.i4 += 1 |
| 134 | +
|
| 135 | + return i |
| 136 | +``` |
| 137 | + |
| 138 | +### Java |
| 139 | + |
| 140 | +```java |
| 141 | +/** |
| 142 | + * The read4 API is defined in the parent class Reader4. |
| 143 | + * int read4(char[] buf4); |
| 144 | + */ |
| 145 | + |
| 146 | +public class Solution extends Reader4 { |
| 147 | + private char[] buf4 = new char[4]; |
| 148 | + private int i4 = 0; |
| 149 | + private int n4 = 0; |
| 150 | + |
| 151 | + /** |
| 152 | + * @param buf Destination buffer |
| 153 | + * @param n Number of characters to read |
| 154 | + * @return The number of actual characters read |
| 155 | + */ |
| 156 | + public int read(char[] buf, int n) { |
| 157 | + int i = 0; |
| 158 | + |
| 159 | + while (i < n) { |
| 160 | + if (i4 == n4) { |
| 161 | + n4 = read4(buf4); |
| 162 | + i4 = 0; |
| 163 | + if (n4 == 0) { |
| 164 | + break; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + while (i < n && i4 < n4) { |
| 169 | + buf[i++] = buf4[i4++]; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return i; |
| 174 | + } |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +### JavaScript |
| 179 | + |
| 180 | +```javascript |
| 181 | +/** |
| 182 | + * Definition for read4 API |
| 183 | + * read4 = function(buf4) { |
| 184 | + * // Reads 4 characters at a time and writes to buf4 |
| 185 | + * // Returns the number of characters actually read |
| 186 | + * } |
| 187 | + */ |
| 188 | + |
| 189 | +var Solution = function () { |
| 190 | + this.buf4 = new Array(4); |
| 191 | + this.i4 = 0; |
| 192 | + this.n4 = 0; |
| 193 | +}; |
| 194 | + |
| 195 | +/** |
| 196 | + * @param {character[]} buf Destination buffer |
| 197 | + * @param {number} n Number of characters to read |
| 198 | + * @return {number} The number of actual characters read |
| 199 | + */ |
| 200 | +Solution.prototype.read = function (buf, n) { |
| 201 | + let i = 0; |
| 202 | + |
| 203 | + while (i < n) { |
| 204 | + if (this.i4 === this.n4) { |
| 205 | + this.n4 = read4(this.buf4); |
| 206 | + this.i4 = 0; |
| 207 | + if (this.n4 === 0) { |
| 208 | + break; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + while (i < n && this.i4 < this.n4) { |
| 213 | + buf[i++] = this.buf4[this.i4++]; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return i; |
| 218 | +}; |
| 219 | +``` |
0 commit comments