-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path3622.read-n-characters-given-read4.py
122 lines (112 loc) · 2.59 KB
/
3622.read-n-characters-given-read4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Tag: Simulation
# Time: O(N)
# Space: O(1)
# Ref: Leetcode-157
# Note: -
# 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.
#
# You cannot access the file directly, you need to read the file indirectly through the `read4()` function.
# 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.
# Similarly, this function will also return the length of the string actually read.
#
# You can see more explanation in the Example.
#
# **Example 1**
#
# Input:
#
# ```plaintext
# "lintcode"
# 5
# ```
#
# Output:
#
# ```plaintext
# 5
# lintc
# ```
#
# Explanation:
#
# 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.
#
# **Example 2**
#
# Input:
#
# ```plaintext
# "lintcode"
# 9
# ```
#
# Output:
#
# ```plaintext
# 8
# lintcode
# ```
#
# **Example 3**
#
# Input:
#
# ```plaintext
# "lintcode"
# 0
# ```
#
# Output:
#
# ```plaintext
# 0
# ""
# ```
# Note: `""` is actually an empty string.
#
#
from typing import (
List,
)
# You can use read4() function by self.read4()
class Solution(Reader):
"""
@param buf: destination
@param n: the number of characters that need to be read
@return: the number of characters read
"""
def read(self, buf: List[str], n: int) -> int:
# write you code here
total = 0
tmp = [''] * 4
while total < n:
count = self.read4(tmp)
if count == 0:
break
read = min(count, n - total)
for i in range(read):
buf[total + i] = tmp[i]
total += read
return total
# You can use read4() function by self.read4()
class Solution(Reader):
"""
@param buf: destination
@param n: the number of characters that need to be read
@return: the number of characters read
"""
def read(self, buf: List[str], n: int) -> int:
# write you code here
tmp = [''] * 4
offset = 0
count = 0
for i in range(n):
if offset == count:
count = self.read4(tmp)
offset = 0
if offset < count:
buf[i] = tmp[offset]
offset += 1
else:
return i
return n