-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path660.read-n-characters-given-read4-ii-call-multiple-times.py
81 lines (73 loc) · 1.74 KB
/
660.read-n-characters-given-read4-ii-call-multiple-times.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
# Tag: String, Simulation
# Time: O(N)
# Space: O(1)
# Ref: Leetcode-158
# Note: -
# The API: `int read4(char *buf)` reads `4` characters at a time from a file.
#
# The return value is the actual number of characters read.
# For example, it returns 3 if there is only 3 characters left in the file.
#
# By using the read4 API, implement the function `int read(char *buf, int n)` that reads n characters from the file.
#
# **Example 1**
#
# ```plain
# Input:
# "filetestbuffer"
# read(6)
# read(5)
# read(4)
# read(3)
# read(2)
# read(1)
# read(10)
# Output:
# 6, buf = "filete"
# 5, buf = "stbuf"
# 3, buf = "fer"
# 0, buf = ""
# 0, buf = ""
# 0, buf = ""
# 0, buf = ""
# ```
#
# **Example 2**
#
# ```plain
# Input:
# "abcdef"
# read(1)
# read(5)
# Output:
# 1, buf = "a"
# 5, buf = "bcdef"
# ```
#
# The `read` function may be called multiple times.
"""
The read4 API is already defined for you.
@param buf a list of characters
@return an integer
you can call Reader.read4(buf)
"""
class Solution:
# @param {char[]} buf destination buffer
# @param {int} n maximum number of characters to read
# @return {int} the number of characters read
def __init__(self):
self.offset = 0
self.count = 0
self.tmp = [''] * 4
def read(self, buf, n):
# Write your code here
for i in range(n):
if self.offset == self.count:
self.count = Reader.read4(self.tmp)
self.offset = 0
if self.offset < self.count:
buf[i] = self.tmp[self.offset]
self.offset += 1
else:
return i
return n