Skip to content

Commit edeb912

Browse files
committed
feat: add solutions to lc problem: No.0157
No.0157.Read N Characters Given Read4
1 parent 6af8ef1 commit edeb912

File tree

6 files changed

+378
-2
lines changed

6 files changed

+378
-2
lines changed

solution/0100-0199/0157.Read N Characters Given Read4/README.md

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,154 @@ read4(buf4); // read4 返回 0。现在 buf = "",fp 指向文件末
9191

9292
<!-- 这里可写通用的实现逻辑 -->
9393

94+
**方法一:模拟**
95+
96+
直接模拟读取文件的过程,每次读取 4 个字符,然后将读取的字符存入缓存数组中,直到读取的字符数目达到 n 或者文件读取完毕。
97+
98+
时间复杂度 $O(n)$。其中 $n$ 为要读取的字符数目。
99+
94100
<!-- tabs:start -->
95101

96102
### **Python3**
97103

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

100106
```python
101-
107+
"""
108+
The read4 API is already defined for you.
109+
110+
@param buf4, a list of characters
111+
@return an integer
112+
def read4(buf4):
113+
114+
# Below is an example of how the read4 API can be called.
115+
file = File("abcdefghijk") # File is "abcdefghijk", initially file pointer (fp) points to 'a'
116+
buf4 = [' '] * 4 # Create buffer with enough space to store characters
117+
read4(buf4) # read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'
118+
read4(buf4) # read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'
119+
read4(buf4) # read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file
120+
"""
121+
122+
123+
class Solution:
124+
def read(self, buf, n):
125+
"""
126+
:type buf: Destination buffer (List[str])
127+
:type n: Number of characters to read (int)
128+
:rtype: The number of actual characters read (int)
129+
"""
130+
i = 0
131+
buf4 = [0] * 4
132+
v = 5
133+
while v >= 4:
134+
v = read4(buf4)
135+
for j in range(v):
136+
buf[i] = buf4[j]
137+
i += 1
138+
if i >= n:
139+
return n
140+
return i
102141
```
103142

104143
### **Java**
105144

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

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

112244
### **...**

solution/0100-0199/0157.Read N Characters Given Read4/README_EN.md

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,139 @@ Note that &quot;abc&quot; is the file&#39;s content, not buf. buf is the destina
100100
### **Python3**
101101

102102
```python
103-
103+
"""
104+
The read4 API is already defined for you.
105+
106+
@param buf4, a list of characters
107+
@return an integer
108+
def read4(buf4):
109+
110+
# Below is an example of how the read4 API can be called.
111+
file = File("abcdefghijk") # File is "abcdefghijk", initially file pointer (fp) points to 'a'
112+
buf4 = [' '] * 4 # Create buffer with enough space to store characters
113+
read4(buf4) # read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'
114+
read4(buf4) # read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'
115+
read4(buf4) # read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file
116+
"""
117+
118+
119+
class Solution:
120+
def read(self, buf, n):
121+
"""
122+
:type buf: Destination buffer (List[str])
123+
:type n: Number of characters to read (int)
124+
:rtype: The number of actual characters read (int)
125+
"""
126+
i = 0
127+
buf4 = [0] * 4
128+
v = 5
129+
while v >= 4:
130+
v = read4(buf4)
131+
for j in range(v):
132+
buf[i] = buf4[j]
133+
i += 1
134+
if i >= n:
135+
return n
136+
return i
104137
```
105138

106139
### **Java**
107140

108141
```java
142+
/**
143+
* The read4 API is defined in the parent class Reader4.
144+
* int read4(char[] buf4);
145+
*/
146+
147+
public class Solution extends Reader4 {
148+
/**
149+
* @param buf Destination buffer
150+
* @param n Number of characters to read
151+
* @return The number of actual characters read
152+
*/
153+
public int read(char[] buf, int n) {
154+
char[] buf4 = new char[4];
155+
int i = 0, v = 5;
156+
while (v >= 4) {
157+
v = read4(buf4);
158+
for (int j = 0; j < v; ++j) {
159+
buf[i++] = buf4[j];
160+
if (i >= n) {
161+
return n;
162+
}
163+
}
164+
}
165+
return i;
166+
}
167+
}
168+
```
169+
170+
### **C++**
171+
172+
```cpp
173+
/**
174+
* The read4 API is defined in the parent class Reader4.
175+
* int read4(char *buf4);
176+
*/
177+
178+
class Solution {
179+
public:
180+
/**
181+
* @param buf Destination buffer
182+
* @param n Number of characters to read
183+
* @return The number of actual characters read
184+
*/
185+
int read(char *buf, int n) {
186+
char buf4[4];
187+
int i = 0, v = 5;
188+
while (v >= 4) {
189+
v = read4(buf4);
190+
for (int j = 0; j < v; ++j) {
191+
buf[i++] = buf4[j];
192+
if (i >= n) {
193+
return n;
194+
}
195+
}
196+
}
197+
return i;
198+
}
199+
};
200+
```
109201
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+
// implement read below.
220+
return func(buf []byte, n int) int {
221+
buf4 := make([]byte, 4)
222+
i, v := 0, 5
223+
for v >= 4 {
224+
v = read4(buf4)
225+
for j := 0; j < v; j++ {
226+
buf[i] = buf4[j]
227+
i++
228+
if i >= n {
229+
return n
230+
}
231+
}
232+
}
233+
return i
234+
}
235+
}
110236
```
111237

112238
### **...**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
char buf4[4];
15+
int i = 0, v = 5;
16+
while (v >= 4) {
17+
v = read4(buf4);
18+
for (int j = 0; j < v; ++j) {
19+
buf[i++] = buf4[j];
20+
if (i >= n) {
21+
return n;
22+
}
23+
}
24+
}
25+
return i;
26+
}
27+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
// implement read below.
16+
return func(buf []byte, n int) int {
17+
buf4 := make([]byte, 4)
18+
i, v := 0, 5
19+
for v >= 4 {
20+
v = read4(buf4)
21+
for j := 0; j < v; j++ {
22+
buf[i] = buf4[j]
23+
i++
24+
if i >= n {
25+
return n
26+
}
27+
}
28+
}
29+
return i
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
/**
8+
* @param buf Destination buffer
9+
* @param n Number of characters to read
10+
* @return The number of actual characters read
11+
*/
12+
public int read(char[] buf, int n) {
13+
char[] buf4 = new char[4];
14+
int i = 0, v = 5;
15+
while (v >= 4) {
16+
v = read4(buf4);
17+
for (int j = 0; j < v; ++j) {
18+
buf[i++] = buf4[j];
19+
if (i >= n) {
20+
return n;
21+
}
22+
}
23+
}
24+
return i;
25+
}
26+
}

0 commit comments

Comments
 (0)