Leetcode 157/158 Read N Characters Given Read4

FB高频题

157: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.

class Solution {
public:
    int read(char *buf, int n) 
    {
    	int res = 0;
        for(int i = 0; i <= n/4; i++)
        {
        	int currd = read4(buf + res);
        	if(currd == 0) break;
        	res = res + currd;
        }
		return min(res, n);
    }
};   

Unit test:
when n = 16, i loops 5 times.
0, read 4
1, read 4
2, read 4,
3, read 4
4, currd = 0, exit loop
res = n = 16

when n = 17, i loops 5 times.
4, read 1, currd = 1, res = 17
5, exit
if the file is only 12 chars, then at loop
3, currd = 0, exit loop.
res = 12, n = 17
min(res, n) returns 12.

158:Read N Characters Given Read4 II - Call multiple times
The read function may be called multiple times.
Input

"filetestbuffer"
read(6)
read(5)
read(4)
read(3)
read(2)
read(1)
read(10)

Expected
6, buf = "filete"
5, buf = "stbuf"
3, buf = "fer"
0, buf = ""
0, buf = ""
0, buf = ""
0, buf = ""

难点是重复调用的read需要接着前面的read,而不是从新开始。意味着上次read4读出来的char要存下来,下次read的时候从里面先拿,然后再重新read4.
所以至少要存:
一个char[4] buffer4,
read4()返回的地址(个数),
上次buffer4 read的地址。

// 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) {
        for(int i = 0; i < n; i++)
        {
        	//init, rd4 and rd == 0
        	//after first loop, rd4 becomes 4,
        	// next time when rd == rd4, means all the 4 char has been read and save to buf. And new rd starts.
        	// if the goal buf is full, terminated by i < n
            if(rd4 == rd)
            {
            	//read4 and save to buffer4 and reset the rd to 0
                rd4 = read4(tempbuf);
                rd = 0;
                //exit when rd4 is 0 which means the file is empty
                if(rd4 == 0) return i;
            }         
            buf[i] = tempbuf[rd++];
        }
    }
    
private:
        char tempbuf[4];
        int rd4 = 0;
        int rd = 0;

};

另一种写法更容易懂:

// 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) {
        bool isend = false;
        int index = 0;
        
        while(!isend && index < n)
        {
            if(rd4 == 0)
            {
                rd4 = read4(tempbuf);
                if(rd4 < 4)
                {
                    isend = true;
                }
            }
            
            while(rd4 > 0 && index < n)
            {
                buf[index ++] = tempbuf[rd++];
                rd = rd & 0x3;
                rd4 --;
            }
        }
        
        return index;
    }
    
private:
    char tempbuf[4];
    int rd4 = 0;
    int rd = 0;
};

猜你喜欢

转载自blog.csdn.net/weixin_43476349/article/details/83776829