Skip to content

Commit f3a7e8e

Browse files
committed
udpate
1 parent 8dc4f9f commit f3a7e8e

7 files changed

+281
-85
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ 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) | 11/51 | 1 vip |
54+
| [🔲] | [9c-top.md](./list/9c-top.md) | 12/52 | 1 vip |
5555

56-
**Solved**: 480 problems
56+
**Solved**: 481 problems
5757

5858
## 类型/Category
5959

@@ -173,7 +173,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
173173

174174
## Simulation
175175

176-
| Link | Problem(12) | Solution | Tag | Time | Space | Ref |
176+
| Link | Problem(14) | Solution | Tag | Time | Space | Ref |
177177
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
178178
| [Leetcode-67](https://leetcode.com/problems/add-binary/) | Add Binary | [c++](./leetcode/67.add-binary.cpp), [python3](./leetcode/67.add-binary.py) | Simulation | O\(M\+N\) | O\(1\) | - |
179179
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Simulation | O\(N\) | O\(1\) | - |
@@ -184,6 +184,8 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
184184
| [Lintcode-366](https://www.lintcode.com/problem/fibonacci/) | Fibonacci | [c++](./lintcode/366.fibonacci.cpp), [python3](./lintcode/366.fibonacci.py) | Simulation | O\(N\) | O\(N\) | - |
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 |
187+
| [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 | \- | \- | - |
187189
| [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\) | - |
188190
| [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 |
189191
| [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 |
@@ -916,7 +918,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
916918

917919
## Other
918920

919-
| Link | Problem(129) | Solution | Tag | Time | Space | Ref |
921+
| Link | Problem(128) | Solution | Tag | Time | Space | Ref |
920922
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
921923
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
922924
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -1004,7 +1006,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
10041006
| [Leetcode-66](https://leetcode.com/problems/plus-one/) | Plus One | [c++](./leetcode/66.plus-one.cpp), [python3](./leetcode/66.plus-one.py) | Other | \- | \- | - |
10051007
| [Leetcode-755](https://leetcode.com/problems/pour-water/) | Pour Water | [python3](./leetcode/755.pour-water.py) | Other | \- | \- | - |
10061008
| [Leetcode-370](https://leetcode.com/problems/range-addition/) | Range Addition | [c++](./leetcode/370.range-addition.cpp), [python3](./leetcode/370.range-addition.py) | Other | \- | \- | - |
1007-
| [Leetcode-157](https://leetcode.com/problems/read-n-characters-given-read4/) | Read N Characters Given Read4 | [c++](./leetcode/157.read-n-characters-given-read4.cpp), [python3](./leetcode/157.read-n-characters-given-read4.py) | Other | \- | \- | - |
10081009
| [Leetcode-26](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | Remove Duplicates From Sorted Array | [c++](./leetcode/26.remove-duplicates-from-sorted-array.cpp), [python3](./leetcode/26.remove-duplicates-from-sorted-array.py) | Other | \- | \- | - |
10091010
| [Leetcode-27](https://leetcode.com/problems/remove-element/) | Remove Element | [c++](./leetcode/27.remove-element.cpp), [python3](./leetcode/27.remove-element.py) | Other | \- | \- | - |
10101011
| [Leetcode-203](https://leetcode.com/problems/remove-linked-list-elements/) | Remove Linked List Elements | [python3](./leetcode/203.remove-linked-list-elements.py) | Other | \- | \- | - |

leetcode/157.read-n-characters-given-read4.cpp

Lines changed: 0 additions & 35 deletions
This file was deleted.

leetcode/157.read-n-characters-given-read4.py

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Tag: Simulation
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: Leetcode-157
5+
// Note: -
6+
7+
// In this question, you need to design the `read()` function to read and save the first n characters of the file into `buf`, and return the length of the read string.
8+
//
9+
// You cannot access the file directly, you need to read the file indirectly through the `read4()` function.
10+
// Among them, the `read4()` function reads 4 characters at a time, and if it is called multiple times, it will continue to read from the last read result.
11+
// Similarly, this function will also return the length of the string actually read.
12+
//
13+
// You can see more explanation in the Example.
14+
//
15+
// **Example 1**
16+
//
17+
// Input:
18+
//
19+
// ```plaintext
20+
// "lintcode"
21+
// 5
22+
// ```
23+
//
24+
// Output:
25+
//
26+
// ```plaintext
27+
// 5
28+
// lintc
29+
// ```
30+
//
31+
// Explanation:
32+
//
33+
// The content of file is **lintcode**, the first call to `read4()` reads **lint**, and the second call only needs to read the fifth character (**c**). So the final read string is **lintc** with a length of 5.
34+
//
35+
// **Example 2**
36+
//
37+
// Input:
38+
//
39+
// ```plaintext
40+
// "lintcode"
41+
// 9
42+
// ```
43+
//
44+
// Output:
45+
//
46+
// ```plaintext
47+
// 8
48+
// lintcode
49+
// ```
50+
//
51+
// **Example 3**
52+
//
53+
// Input:
54+
//
55+
// ```plaintext
56+
// "lintcode"
57+
// 0
58+
// ```
59+
//
60+
// Output:
61+
//
62+
// ```plaintext
63+
// 0
64+
// ""
65+
// ```
66+
// Note: `""` is actually an empty string.
67+
//
68+
//
69+
70+
int read4(char* buf);
71+
72+
class Solution {
73+
public:
74+
/**
75+
* @param buf: destination
76+
* @param n: the number of characters that need to be read
77+
* @return: the number of characters read
78+
*/
79+
80+
int read(char* buf, int n) {
81+
// write you code here
82+
int total = 0;
83+
char tmp[4];
84+
while (total < n) {
85+
int count = read4(tmp);
86+
if (count == 0) {
87+
break;
88+
}
89+
int read = min(count, n - total);
90+
for (int i = 0; i < read; i++) {
91+
buf[total + i] = tmp[i];
92+
}
93+
total += read;
94+
}
95+
return total;
96+
}
97+
};
98+
99+
int read4(char* buf);
100+
101+
class Solution {
102+
public:
103+
/**
104+
* @param buf: destination
105+
* @param n: the number of characters that need to be read
106+
* @return: the number of characters read
107+
*/
108+
109+
int read(char* buf, int n) {
110+
// write you code here
111+
char tmp[4];
112+
int offset = 0;
113+
int read = 0;
114+
115+
for(auto i = 0; i < n; ++i) {
116+
if (offset == read) {
117+
read = read4(tmp);
118+
offset = 0;
119+
}
120+
121+
if (offset < read) {
122+
buf[i] = tmp[offset++];
123+
} else {
124+
return i;
125+
}
126+
}
127+
128+
return n;
129+
}
130+
};
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Tag: Simulation
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: Leetcode-157
5+
# Note: -
6+
7+
# In this question, you need to design the `read()` function to read and save the first n characters of the file into `buf`, and return the length of the read string.
8+
#
9+
# You cannot access the file directly, you need to read the file indirectly through the `read4()` function.
10+
# Among them, the `read4()` function reads 4 characters at a time, and if it is called multiple times, it will continue to read from the last read result.
11+
# Similarly, this function will also return the length of the string actually read.
12+
#
13+
# You can see more explanation in the Example.
14+
#
15+
# **Example 1**
16+
#
17+
# Input:
18+
#
19+
# ```plaintext
20+
# "lintcode"
21+
# 5
22+
# ```
23+
#
24+
# Output:
25+
#
26+
# ```plaintext
27+
# 5
28+
# lintc
29+
# ```
30+
#
31+
# Explanation:
32+
#
33+
# The content of file is **lintcode**, the first call to `read4()` reads **lint**, and the second call only needs to read the fifth character (**c**). So the final read string is **lintc** with a length of 5.
34+
#
35+
# **Example 2**
36+
#
37+
# Input:
38+
#
39+
# ```plaintext
40+
# "lintcode"
41+
# 9
42+
# ```
43+
#
44+
# Output:
45+
#
46+
# ```plaintext
47+
# 8
48+
# lintcode
49+
# ```
50+
#
51+
# **Example 3**
52+
#
53+
# Input:
54+
#
55+
# ```plaintext
56+
# "lintcode"
57+
# 0
58+
# ```
59+
#
60+
# Output:
61+
#
62+
# ```plaintext
63+
# 0
64+
# ""
65+
# ```
66+
# Note: `""` is actually an empty string.
67+
#
68+
#
69+
70+
from typing import (
71+
List,
72+
)
73+
74+
# You can use read4() function by self.read4()
75+
class Solution(Reader):
76+
"""
77+
@param buf: destination
78+
@param n: the number of characters that need to be read
79+
@return: the number of characters read
80+
"""
81+
def read(self, buf: List[str], n: int) -> int:
82+
# write you code here
83+
total = 0
84+
tmp = [''] * 4
85+
while total < n:
86+
count = self.read4(tmp)
87+
if count == 0:
88+
break
89+
90+
read = min(count, n - total)
91+
for i in range(read):
92+
buf[total + i] = tmp[i]
93+
total += read
94+
95+
return total
96+
97+
# You can use read4() function by self.read4()
98+
class Solution(Reader):
99+
"""
100+
@param buf: destination
101+
@param n: the number of characters that need to be read
102+
@return: the number of characters read
103+
"""
104+
def read(self, buf: List[str], n: int) -> int:
105+
# write you code here
106+
tmp = [''] * 4
107+
offset = 0
108+
read = 0
109+
110+
for i in range(n):
111+
if offset == read:
112+
read = self.read4(tmp)
113+
offset = 0
114+
115+
if offset < read:
116+
buf[i] = tmp[offset]
117+
offset += 1
118+
else:
119+
return i
120+
121+
return n
122+

list/9c-top.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
10. https://www.lintcode.com/problem/segment-tree-modify/
1818
11. https://www.lintcode.com/problem/strobogrammatic-number/
1919
12. https://www.lintcode.com/problem/one-edit-distance/
20+
13. https://www.lintcode.com/problem/read-n-characters-given-read4/
2021

2122
- https://www.lintcode.com/problem/read-characters-from-file-multiple-calls/
2223
- https://www.lintcode.com/problem/strings-serialization/

0 commit comments

Comments
 (0)