157. Read N Characters Given Read4

https://leetcode.com/problems/read-n-characters-given-read4/#/description

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.

Note:
The read function will only be called once for each test case.

Hint:

We set empty list buf4 that reads 4 chars and write them to buf, when the index meets certain conditions.

Don‘t worry about if n is the multiple of 4 or not, no need to use mod function. (I guess I am too familar wth it?)

Sol 1:

def read(self, buf, n):
    idx = 0
    while n > 0:
        # read file to buf4
        buf4 = [""]*4
        l = read4(buf4)
        # if no more char in file, return
        if not l:
            return idx
        # write buf4 into buf directly
        for i in range(min(l, n)):
            buf[idx] = buf4[i]
            idx += 1
            n -= 1
    return idx

Sol 2:

# The read4 API is already defined for you.
# @param buf, a list of characters
# @return an integer
# def read4(buf):

class Solution(object):
    def read(self, buf, n):
        """
        :type buf: Destination buffer (List[str])
        :type n: Maximum number of characters to read (int)
        :rtype: The number of characters read (int)
        """
        idx = 0
        while True:
            buf4 = [""] *4
            curr = min(read4(buf4), n-idx) # curr is the number of chars that reads
            for i in range(curr):
                buf[idx] = buf4[i]
                idx += 1
                         # if curr != 4 or idx == n             if curr < 4: # return if it reaches the end of file or reaches n
                return idx

Notes:

1 In python, "while True" is used to start a loop when the if condition at the end of the loop is met. 

2 [""] * 4 is  [‘‘, ‘‘, ‘‘, ‘‘]

3 curr variable keeps track of the index of chars read so far, it is the min of read4(buf4) and n - index, where n is the total len of chars and index is the number of chars has read so far. Output is the index variable.

The min comparison is tricky here, because it ensures all chars are read within n scope.

4 The condition to end the while loop is when curr is smaller than 4 or not equal to 4.

The previous step above the end condition is to wirte the last four chars into buf[idx]  

https://discuss.leetcode.com/topic/8474/9-line-61ms-ac-python-solution-with-comments

Sol 3:

    def read(self, buf, n):
        read, need, buffer = 0, n, [‘‘]*4
        while need > 0:
            k = read4(buffer)
            need = min(k, n - read)
            buf[read:read+need] = buffer[:need]
            read += need
        return read
时间: 2025-01-18 11:00:18

157. Read N Characters Given Read4的相关文章

leetcode[157] Read N Characters Given Read4

题目意思是给你一个read4的函数,实现一个readn的函数. 一开始题目一直没搞懂,原来,read4(buf) 是指,读4个字符存到buf,或者读剩下的不足四个的字符,返回的数字是存到buf里的字符数. 没有买书,只能网上看看别人分享的代码: // Forward declaration of the read4 API. int read4(char *buf); class Solution { public: /** * @param buf Destination buffer * @

leetcode 157. Read N Characters Given Read4 利用read4实现read --------- java

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

[LeetCode] 158. Read N Characters Given Read4 II - Call multiple times

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

[Locked] Read N Characters Given Read4 &amp; Read N Characters Given Read4 II - Call multiple times

Read N Characters Given Read4 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 rea

[LeetCode] Read N Characters Given Read4 I &amp; II

Read N Characters Given Read4 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 rea

[leetcode]Read N Characters Given Read4

用read4实现readn... 至调用一次,感觉怎么搞都可以...估计这个题有II就是调用多次了... 感觉多次勇哥buffer存下多读的那部分就好了... // Forward declaration of the read4 API. int read4(char *buf); class Solution { public: /** * @param buf Destination buffer * @param n Maximum number of characters to rea

Leetcode-Read N Characters Given Read4 II

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

LeetCode-Read N Characters Given Read4 II - Call multiple times

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

Read N Characters Given Read4 II - Call multiple times

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