You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[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 |\-|\-| - |
|[Leetcode-66](https://leetcode.com/problems/plus-one/)| Plus One |[c++](./leetcode/66.plus-one.cpp), [python3](./leetcode/66.plus-one.py)| Other |\-|\-| - |
1005
1007
|[Leetcode-755](https://leetcode.com/problems/pour-water/)| Pour Water |[python3](./leetcode/755.pour-water.py)| Other |\-|\-| - |
1006
1008
|[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 |\-|\-| - |
1008
1009
|[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 |\-|\-| - |
1009
1010
|[Leetcode-27](https://leetcode.com/problems/remove-element/)| Remove Element |[c++](./leetcode/27.remove-element.cpp), [python3](./leetcode/27.remove-element.py)| Other |\-|\-| - |
1010
1011
|[Leetcode-203](https://leetcode.com/problems/remove-linked-list-elements/)| Remove Linked List Elements |[python3](./leetcode/203.remove-linked-list-elements.py)| Other |\-|\-| - |
// 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
+
intread4(char* buf);
71
+
72
+
classSolution {
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
+
intread(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
+
intread = 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
+
intread4(char* buf);
100
+
101
+
classSolution {
102
+
public:
103
+
/**
104
+
* @param buf: destination
105
+
* @param n: the number of characters that need to be read
# 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
+
fromtypingimport (
71
+
List,
72
+
)
73
+
74
+
# You can use read4() function by self.read4()
75
+
classSolution(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
+
defread(self, buf: List[str], n: int) ->int:
82
+
# write you code here
83
+
total=0
84
+
tmp= [''] *4
85
+
whiletotal<n:
86
+
count=self.read4(tmp)
87
+
ifcount==0:
88
+
break
89
+
90
+
read=min(count, n-total)
91
+
foriinrange(read):
92
+
buf[total+i] =tmp[i]
93
+
total+=read
94
+
95
+
returntotal
96
+
97
+
# You can use read4() function by self.read4()
98
+
classSolution(Reader):
99
+
"""
100
+
@param buf: destination
101
+
@param n: the number of characters that need to be read
0 commit comments