@@ -104,13 +104,139 @@ sol.read(buf, 1); // We have reached the end of file, no more characters can be
104
104
### ** Python3**
105
105
106
106
``` python
107
-
107
+ # The read4 API is already defined for you.
108
+ # def read4(buf4: List[str]) -> int:
109
+
110
+ class Solution :
111
+ def __init__ (self ):
112
+ self .buf4 = [None ] * 4
113
+ self .i = self .size = 0
114
+
115
+ def read (self , buf : List[str ], n : int ) -> int :
116
+ j = 0
117
+ while j < n:
118
+ if self .i == self .size:
119
+ self .size = read4(self .buf4)
120
+ self .i = 0
121
+ if self .size == 0 :
122
+ break
123
+ while j < n and self .i < self .size:
124
+ buf[j] = self .buf4[self .i]
125
+ self .i += 1
126
+ j += 1
127
+ return j
108
128
```
109
129
110
130
### ** Java**
111
131
112
132
``` java
133
+ /**
134
+ * The read4 API is defined in the parent class Reader4.
135
+ * int read4(char[] buf4);
136
+ */
137
+
138
+ public class Solution extends Reader4 {
139
+ private char [] buf4 = new char [4 ];
140
+ private int i;
141
+ private int size;
142
+
143
+ /**
144
+ * @param buf Destination buffer
145
+ * @param n Number of characters to read
146
+ * @return The number of actual characters read
147
+ */
148
+ public int read (char [] buf , int n ) {
149
+ int j = 0 ;
150
+ while (j < n) {
151
+ if (i == size) {
152
+ size = read4(buf4);
153
+ i = 0 ;
154
+ if (size == 0 ) {
155
+ break ;
156
+ }
157
+ }
158
+ while (j < n && i < size) {
159
+ buf[j++ ] = buf4[i++ ];
160
+ }
161
+ }
162
+ return j;
163
+ }
164
+ }
165
+ ```
166
+
167
+ ### ** C++**
168
+
169
+ ``` cpp
170
+ /* *
171
+ * The read4 API is defined in the parent class Reader4.
172
+ * int read4(char *buf4);
173
+ */
174
+
175
+ class Solution {
176
+ public:
177
+ /**
178
+ * @param buf Destination buffer
179
+ * @param n Number of characters to read
180
+ * @return The number of actual characters read
181
+ * /
182
+ int read(char * buf, int n) {
183
+ int j = 0;
184
+ while (j < n) {
185
+ if (i == size) {
186
+ size = read4(buf4);
187
+ i = 0;
188
+ if (size == 0) break;
189
+ }
190
+ while (j < n && i < size) buf[ j++] = buf4[ i++] ;
191
+ }
192
+ return j;
193
+ }
194
+
195
+ private:
196
+ char * buf4 = new char[ 4] ;
197
+ int i = 0;
198
+ int size = 0;
199
+ };
200
+ ```
113
201
202
+ ### **Go**
203
+
204
+ ```go
205
+ /**
206
+ * The read4 API is already defined for you.
207
+ *
208
+ * read4 := func(buf4 []byte) int
209
+ *
210
+ * // Below is an example of how the read4 API can be called.
211
+ * file := File("abcdefghijk") // File is "abcdefghijk", initially file pointer (fp) points to 'a'
212
+ * buf4 := make([]byte, 4) // Create buffer with enough space to store characters
213
+ * read4(buf4) // read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'
214
+ * read4(buf4) // read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'
215
+ * read4(buf4) // read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file
216
+ */
217
+
218
+ var solution = func(read4 func([]byte) int) func([]byte, int) int {
219
+ buf4 := make([]byte, 4)
220
+ i, size := 0, 0
221
+ // implement read below.
222
+ return func(buf []byte, n int) int {
223
+ j := 0
224
+ for j < n {
225
+ if i == size {
226
+ size = read4(buf4)
227
+ i = 0
228
+ if size == 0 {
229
+ break
230
+ }
231
+ }
232
+ for j < n && i < size {
233
+ buf[j] = buf4[i]
234
+ i, j = i+1, j+1
235
+ }
236
+ }
237
+ return j
238
+ }
239
+ }
114
240
```
115
241
116
242
### ** TypeScript**
0 commit comments