zoukankan      html  css  js  c++  java
  • 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]. Write four chars each time. Thus, when the current chars are less than four, it means all chars(1 or 2 or 3 chars) has been written in the last time.  No need to continue the loop. Here we are, break loop condition!

     
     
     
     
     
    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
  • 相关阅读:
    在vi或者vim中显示行号
    Ubuntu 14.10 下进程实时IO监控iotop命令详解
    Ubuntu 14.10 下网络流量实时监控ifstat iftop命令详解
    Ubuntu 14.10 下运行进程实时监控pidstat命令详解
    Ubuntu 14.10 下CPU实时监控mpstat命令详解
    Ubuntu 14.10 下awk命令详解
    Ubuntu 14.10 下sort,uniq,cut,wc命令详解
    Ubuntu 14.10 下sed命令详解
    Ubuntu 14.10 下grep命令详解
    Ubuntu 14.10 下Ganglia监控Spark集群
  • 原文地址:https://www.cnblogs.com/prmlab/p/6939536.html
Copyright © 2011-2022 走看看