类型:字符串
-
- 用 Read4 读取 N 个字符 💚
https://leetcode-cn.com/problems/read-n-characters-given-read4/
❓ 给你一个文件,并且该文件只能通过给定的 read4 方法来读取,请实现一个方法使其能够读取 n 个字符。API read4 可以从文件中读取 4 个连续的字符,并且将它们写入缓存数组 buf 中。
💡 迭代
class Solution: def read(self, buf, n): index = -1 while n > 0: buf4 = [' '] * 4 count = read4(buf4) for i in range(min(count, n)): index += 1 buf[index] = buf4[i] n -= 1 if count < 4: return index + 1 return index + 1时间复杂度:O(n),空间复杂度:O(1)