LeetCode 题解(233) : 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 read4 API, implement the function int read(char *buf, int n) that readsn characters from the file.

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

题解:

Python的时候注意temp = [""] * 4是装字符的list,而不是[]。

C++版:

// Forward declaration of the read4 API.
int read4(char *buf);

class Solution {
public:
    /**
     * @param buf Destination buffer
     * @param n   Maximum number of characters to read
     * @return    The number of characters read
     */
    int read(char *buf, int n) {
        if(n == 0)
            return 0;
        
        int n1 = n / 4;
        int total = 0;
        char *temp = new char[4];
        if(n1 != 0) {
            for(int i = 0; i < n1; i++) {
                int count = read4(temp);
                total += count;
                for(int j = 0; j < count; j++)
                    buf[i * 4 + j] = temp[j];
            }
            if(total < n1 * 4)
                return total;
        }
            
        int n2 = n % 4;
        int count = min(n2, read4(temp));
        for(int i = 0; i < count; i++)
            buf[n1 * 4 + i] = temp[i];
        return total + count;
    }
};

Java版:

/* The read4 API is defined in the parent class Reader4.
      int read4(char[] buf); */

public class Solution extends Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Maximum number of characters to read
     * @return    The number of characters read
     */
    public int read(char[] buf, int n) {
        if(n == 0)
            return 0;
        
        int n1 = n / 4;
        int total = 0;
        char[] temp = new char[4];
        if(n1 != 0) {
            for(int i = 0; i < n1; i++) {
                int count = read4(temp);
                for(int j = 0; j < count; j++) {
                    buf[i * 4 + j] = temp[j];
                }
                total += count;
            }
            if(total < n1 * 4)
                return total;
        }
            
        int n2 = n % 4;
        if(n2 != 0) {
            int count = Math.min(n2, read4(temp));
            for(int j = 0; j < count; j++)
                buf[total + j] = temp[j];
            total += count;
        }
        return total;
    }
}

Python版:

# 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)
        """
        if n == 0:
            return 0
        
        n1 = n / 4
        total = 0
        temp = [""] * 4
        if n1 != 0:
            for i in range(n1):
                count = read4(temp)
                for j in range(count):
                    buf[i * 4 + j] = temp[j]
                total += count
            if total < n1 * 4:
                return total
        
        n2 = n % 4;
        if n2 != 0:
            count = min(n2, read4(temp));
            for j in range(count):
                buf[total + j] = temp[j]
            total += count
        return total

你可能感兴趣的:(Algorithm,LeetCode,面试题)