Skip to content

Commit a0df80d

Browse files
committed
add 158
1 parent f3a7e8e commit a0df80d

6 files changed

+172
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5151
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5252
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5353
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
54-
| [🔲] | [9c-top.md](./list/9c-top.md) | 12/52 | 1 vip |
54+
| [🔲] | [9c-top.md](./list/9c-top.md) | 13/52 | 1 vip |
5555

5656
**Solved**: 481 problems
5757

@@ -185,7 +185,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
185185
| [Lintcode-22](https://www.lintcode.com/problem/flatten-list/) | Flatten List | [c++](./lintcode/22.flatten-list.cpp), [python3](./lintcode/22.flatten-list.py) | Simulation | O\(N\) | O\(N\) | - |
186186
| [Lintcode-640](https://www.lintcode.com/problem/one-edit-distance/) | One Edit Distance | [c++](./lintcode/640.one-edit-distance.cpp), [python3](./lintcode/640.one-edit-distance.py) | Simulation | O\(N\) | O\(1\) | Leetcode-161 |
187187
| [Lintcode-3622](https://www.lintcode.com/problem/read-n-characters-given-read4/) | Read N Characters Given Read4 | [c++](./lintcode/3622.read-n-characters-given-read4.cpp), [python3](./lintcode/3622.read-n-characters-given-read4.py) | Simulation | O\(N\) | O\(1\) | Leetcode-157 |
188-
| [Lintcode-660](https://www.lintcode.com/problem/read-n-characters-given-read4-ii-call-multiple-times/) | Read N Characters Given Read4 Ii Call Multiple Times | [c++](./lintcode/660.read-n-characters-given-read4-ii-call-multiple-times.cpp), [python3](./lintcode/660.read-n-characters-given-read4-ii-call-multiple-times.py) | Simulation | \- | \- | - |
188+
| [Lintcode-660](https://www.lintcode.com/problem/read-n-characters-given-read4-ii-call-multiple-times/) | Read N Characters Given Read4 Ii Call Multiple Times | [c++](./lintcode/660.read-n-characters-given-read4-ii-call-multiple-times.cpp), [python3](./lintcode/660.read-n-characters-given-read4-ii-call-multiple-times.py) | Simulation | O\(N\) | O\(1\) | Leetcode-158 |
189189
| [Lintcode-39](https://www.lintcode.com/problem/recover-rotated-sorted-array/) | Recover Rotated Sorted Array | [c++](./lintcode/39.recover-rotated-sorted-array.cpp), [python3](./lintcode/39.recover-rotated-sorted-array.py) | Simulation | O\(N\) | O\(1\) | - |
190190
| [Lintcode-644](https://www.lintcode.com/problem/strobogrammatic-number/) | Strobogrammatic Number | [c++](./lintcode/644.strobogrammatic-number.cpp), [python3](./lintcode/644.strobogrammatic-number.py) | Simulation | O\(N\) | O\(1\) | Leetcode-246 |
191191
| [Lintcode-637](https://www.lintcode.com/problem/valid-word-abbreviation/) | Valid Word Abbreviation | [c++](./lintcode/637.valid-word-abbreviation.cpp), [python3](./lintcode/637.valid-word-abbreviation.py) | Simulation | O\(N\) | O\(1\) | Leetcode-408 |

lintcode/3622.read-n-characters-given-read4.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ class Solution {
110110
// write you code here
111111
char tmp[4];
112112
int offset = 0;
113-
int read = 0;
113+
int count = 0;
114114

115115
for(auto i = 0; i < n; ++i) {
116-
if (offset == read) {
117-
read = read4(tmp);
116+
if (offset == count) {
117+
count = read4(tmp);
118118
offset = 0;
119119
}
120120

121-
if (offset < read) {
121+
if (offset < count) {
122122
buf[i] = tmp[offset++];
123123
} else {
124124
return i;

lintcode/3622.read-n-characters-given-read4.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ def read(self, buf: List[str], n: int) -> int:
105105
# write you code here
106106
tmp = [''] * 4
107107
offset = 0
108-
read = 0
108+
count = 0
109109

110110
for i in range(n):
111-
if offset == read:
112-
read = self.read4(tmp)
111+
if offset == count:
112+
count = self.read4(tmp)
113113
offset = 0
114114

115-
if offset < read:
115+
if offset < count:
116116
buf[i] = tmp[offset]
117117
offset += 1
118118
else:
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Tag: String, Simulation
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: Leetcode-158
5+
// Note: -
6+
7+
// The API: `int read4(char *buf)` reads `4` characters at a time from a file.
8+
//
9+
// The return value is the actual number of characters read.
10+
// For example, it returns 3 if there is only 3 characters left in the file.
11+
//
12+
// By using the read4 API, implement the function `int read(char *buf, int n)` that reads n characters from the file.
13+
//
14+
// **Example 1**
15+
//
16+
// ```plain
17+
// Input:
18+
// "filetestbuffer"
19+
// read(6)
20+
// read(5)
21+
// read(4)
22+
// read(3)
23+
// read(2)
24+
// read(1)
25+
// read(10)
26+
// Output:
27+
// 6, buf = "filete"
28+
// 5, buf = "stbuf"
29+
// 3, buf = "fer"
30+
// 0, buf = ""
31+
// 0, buf = ""
32+
// 0, buf = ""
33+
// 0, buf = ""
34+
// ```
35+
//
36+
// **Example 2**
37+
//
38+
// ```plain
39+
// Input:
40+
// "abcdef"
41+
// read(1)
42+
// read(5)
43+
// Output:
44+
// 1, buf = "a"
45+
// 5, buf = "bcdef"
46+
// ```
47+
//
48+
// The `read` function may be called multiple times.
49+
50+
// Forward declaration of the read4 API.
51+
int read4(char *buf);
52+
53+
class Solution {
54+
public:
55+
/**
56+
* @param buf destination buffer
57+
* @param n maximum number of characters to read
58+
* @return the number of characters read
59+
*/
60+
int count = 0;
61+
int offset = 0;
62+
char tmp[4];
63+
int read(char *buf, int n) {
64+
// Write your code here
65+
int length = 0;
66+
while (length < n) {
67+
if (count == offset) {
68+
count = read4(tmp);
69+
offset = 0;
70+
}
71+
72+
if (offset < count) {
73+
buf[length++] = tmp[offset++];
74+
} else {
75+
break;
76+
}
77+
}
78+
return length;
79+
}
80+
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Tag: String, Simulation
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: Leetcode-158
5+
# Note: -
6+
7+
# The API: `int read4(char *buf)` reads `4` characters at a time from a file.
8+
#
9+
# The return value is the actual number of characters read.
10+
# For example, it returns 3 if there is only 3 characters left in the file.
11+
#
12+
# By using the read4 API, implement the function `int read(char *buf, int n)` that reads n characters from the file.
13+
#
14+
# **Example 1**
15+
#
16+
# ```plain
17+
# Input:
18+
# "filetestbuffer"
19+
# read(6)
20+
# read(5)
21+
# read(4)
22+
# read(3)
23+
# read(2)
24+
# read(1)
25+
# read(10)
26+
# Output:
27+
# 6, buf = "filete"
28+
# 5, buf = "stbuf"
29+
# 3, buf = "fer"
30+
# 0, buf = ""
31+
# 0, buf = ""
32+
# 0, buf = ""
33+
# 0, buf = ""
34+
# ```
35+
#
36+
# **Example 2**
37+
#
38+
# ```plain
39+
# Input:
40+
# "abcdef"
41+
# read(1)
42+
# read(5)
43+
# Output:
44+
# 1, buf = "a"
45+
# 5, buf = "bcdef"
46+
# ```
47+
#
48+
# The `read` function may be called multiple times.
49+
50+
"""
51+
The read4 API is already defined for you.
52+
@param buf a list of characters
53+
@return an integer
54+
you can call Reader.read4(buf)
55+
"""
56+
57+
58+
class Solution:
59+
60+
# @param {char[]} buf destination buffer
61+
# @param {int} n maximum number of characters to read
62+
# @return {int} the number of characters read
63+
def __init__(self):
64+
self.offset = 0
65+
self.count = 0
66+
self.tmp = [''] * 4
67+
68+
def read(self, buf, n):
69+
# Write your code here
70+
for i in range(n):
71+
if self.offset == self.count:
72+
self.count = Reader.read4(self.tmp)
73+
self.offset = 0
74+
75+
if self.offset < self.count:
76+
buf[i] = self.tmp[self.offset]
77+
self.offset += 1
78+
else:
79+
return i
80+
81+
return n

list/9c-top.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
11. https://www.lintcode.com/problem/strobogrammatic-number/
1919
12. https://www.lintcode.com/problem/one-edit-distance/
2020
13. https://www.lintcode.com/problem/read-n-characters-given-read4/
21+
14. https://www.lintcode.com/problem/read-n-characters-given-read4-ii-call-multiple-times/
2122

22-
- https://www.lintcode.com/problem/read-characters-from-file-multiple-calls/
2323
- https://www.lintcode.com/problem/strings-serialization/
2424
- https://www.lintcode.com/problem/binary-tree-serialization/
2525
- https://www.lintcode.com/problem/system-longest-file-path/

0 commit comments

Comments
 (0)