Skip to content

Commit 2c02292

Browse files
committed
feat: add solutions to lc problem: No.0158
No.0158.Read N Characters Given read4 II - Call Multiple Times
1 parent 58e753e commit 2c02292

File tree

6 files changed

+374
-2
lines changed

6 files changed

+374
-2
lines changed

solution/0100-0199/0158.Read N Characters Given read4 II - Call Multiple Times/README.md

+129-1
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,150 @@ sol.read (buf, 1); // 我们已经到达文件的末尾,不能读取更多的
106106

107107
<!-- 这里可写通用的实现逻辑 -->
108108

109+
**方法一:模拟**
110+
109111
<!-- tabs:start -->
110112

111113
### **Python3**
112114

113115
<!-- 这里可写当前语言的特殊实现逻辑 -->
114116

115117
```python
116-
118+
# The read4 API is already defined for you.
119+
# def read4(buf4: List[str]) -> int:
120+
121+
class Solution:
122+
def __init__(self):
123+
self.buf4 = [None] * 4
124+
self.i = self.size = 0
125+
126+
def read(self, buf: List[str], n: int) -> int:
127+
j = 0
128+
while j < n:
129+
if self.i == self.size:
130+
self.size = read4(self.buf4)
131+
self.i = 0
132+
if self.size == 0:
133+
break
134+
while j < n and self.i < self.size:
135+
buf[j] = self.buf4[self.i]
136+
self.i += 1
137+
j += 1
138+
return j
117139
```
118140

119141
### **Java**
120142

121143
<!-- 这里可写当前语言的特殊实现逻辑 -->
122144

123145
```java
146+
/**
147+
* The read4 API is defined in the parent class Reader4.
148+
* int read4(char[] buf4);
149+
*/
150+
151+
public class Solution extends Reader4 {
152+
private char[] buf4 = new char[4];
153+
private int i;
154+
private int size;
155+
156+
/**
157+
* @param buf Destination buffer
158+
* @param n Number of characters to read
159+
* @return The number of actual characters read
160+
*/
161+
public int read(char[] buf, int n) {
162+
int j = 0;
163+
while (j < n) {
164+
if (i == size) {
165+
size = read4(buf4);
166+
i = 0;
167+
if (size == 0) {
168+
break;
169+
}
170+
}
171+
while (j < n && i < size) {
172+
buf[j++] = buf4[i++];
173+
}
174+
}
175+
return j;
176+
}
177+
}
178+
```
179+
180+
### **C++**
181+
182+
```cpp
183+
/**
184+
* The read4 API is defined in the parent class Reader4.
185+
* int read4(char *buf4);
186+
*/
187+
188+
class Solution {
189+
public:
190+
/**
191+
* @param buf Destination buffer
192+
* @param n Number of characters to read
193+
* @return The number of actual characters read
194+
*/
195+
int read(char *buf, int n) {
196+
int j = 0;
197+
while (j < n) {
198+
if (i == size) {
199+
size = read4(buf4);
200+
i = 0;
201+
if (size == 0) break;
202+
}
203+
while (j < n && i < size) buf[j++] = buf4[i++];
204+
}
205+
return j;
206+
}
207+
208+
private:
209+
char *buf4 = new char[4];
210+
int i = 0;
211+
int size = 0;
212+
};
213+
```
124214
215+
### **Go**
216+
217+
```go
218+
/**
219+
* The read4 API is already defined for you.
220+
*
221+
* read4 := func(buf4 []byte) int
222+
*
223+
* // Below is an example of how the read4 API can be called.
224+
* file := File("abcdefghijk") // File is "abcdefghijk", initially file pointer (fp) points to 'a'
225+
* buf4 := make([]byte, 4) // Create buffer with enough space to store characters
226+
* read4(buf4) // read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'
227+
* read4(buf4) // read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'
228+
* read4(buf4) // read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file
229+
*/
230+
231+
var solution = func(read4 func([]byte) int) func([]byte, int) int {
232+
buf4 := make([]byte, 4)
233+
i, size := 0, 0
234+
// implement read below.
235+
return func(buf []byte, n int) int {
236+
j := 0
237+
for j < n {
238+
if i == size {
239+
size = read4(buf4)
240+
i = 0
241+
if size == 0 {
242+
break
243+
}
244+
}
245+
for j < n && i < size {
246+
buf[j] = buf4[i]
247+
i, j = i+1, j+1
248+
}
249+
}
250+
return j
251+
}
252+
}
125253
```
126254

127255
### **TypeScript**

solution/0100-0199/0158.Read N Characters Given read4 II - Call Multiple Times/README_EN.md

+127-1
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,139 @@ sol.read(buf, 1); // We have reached the end of file, no more characters can be
104104
### **Python3**
105105

106106
```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
108128
```
109129

110130
### **Java**
111131

112132
```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+
```
113201
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+
}
114240
```
115241

116242
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* The read4 API is defined in the parent class Reader4.
3+
* int read4(char *buf4);
4+
*/
5+
6+
class Solution {
7+
public:
8+
/**
9+
* @param buf Destination buffer
10+
* @param n Number of characters to read
11+
* @return The number of actual characters read
12+
*/
13+
int read(char *buf, int n) {
14+
int j = 0;
15+
while (j < n) {
16+
if (i == size) {
17+
size = read4(buf4);
18+
i = 0;
19+
if (size == 0) break;
20+
}
21+
while (j < n && i < size) buf[j++] = buf4[i++];
22+
}
23+
return j;
24+
}
25+
26+
private:
27+
char *buf4 = new char[4];
28+
int i = 0;
29+
int size = 0;
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* The read4 API is already defined for you.
3+
*
4+
* read4 := func(buf4 []byte) int
5+
*
6+
* // Below is an example of how the read4 API can be called.
7+
* file := File("abcdefghijk") // File is "abcdefghijk", initially file pointer (fp) points to 'a'
8+
* buf4 := make([]byte, 4) // Create buffer with enough space to store characters
9+
* read4(buf4) // read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'
10+
* read4(buf4) // read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'
11+
* read4(buf4) // read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file
12+
*/
13+
14+
var solution = func(read4 func([]byte) int) func([]byte, int) int {
15+
buf4 := make([]byte, 4)
16+
i, size := 0, 0
17+
// implement read below.
18+
return func(buf []byte, n int) int {
19+
j := 0
20+
for j < n {
21+
if i == size {
22+
size = read4(buf4)
23+
i = 0
24+
if size == 0 {
25+
break
26+
}
27+
}
28+
for j < n && i < size {
29+
buf[j] = buf4[i]
30+
i, j = i+1, j+1
31+
}
32+
}
33+
return j
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* The read4 API is defined in the parent class Reader4.
3+
* int read4(char[] buf4);
4+
*/
5+
6+
public class Solution extends Reader4 {
7+
private char[] buf4 = new char[4];
8+
private int i;
9+
private int size;
10+
11+
/**
12+
* @param buf Destination buffer
13+
* @param n Number of characters to read
14+
* @return The number of actual characters read
15+
*/
16+
public int read(char[] buf, int n) {
17+
int j = 0;
18+
while (j < n) {
19+
if (i == size) {
20+
size = read4(buf4);
21+
i = 0;
22+
if (size == 0) {
23+
break;
24+
}
25+
}
26+
while (j < n && i < size) {
27+
buf[j++] = buf4[i++];
28+
}
29+
}
30+
return j;
31+
}
32+
}

0 commit comments

Comments
 (0)