Read N Characters Given Read4 (1 && 2) | LeetCode

先说1;

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.


理解题意后其实不难,这道题的意思是,有一个文件,读文件用read4这个函数,将文件内容读到buf下。 注意一下,因为一次至少读4个或者余下所有,所以你的buf有可能一读读成了abc, 但是其实我们只想要ab。所以这个时候就要靠我们的返回值了,返回值为2的情况下,leetcode就会认为你的buf里只有ab。而选择性忽略c。

当读的时候会有以下三种情况。

1. 所需要的个数比读进来的树要少。比如文件是abc,但是我想读两个字节。所以这个时候,返回值返回的就不能是你读的个数,而是你所需要的个数。

2. 读进来的数少于4个,这种情况下read4的返回值再加上之前的值就可以了。

3. 一读读进来4个,那就继续读下去。

 其实(2,3可以合并)


代码如下:

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

class Solution {
public:
    int read(char *buf, int n) {
        int result = 0;
        while (n > 0) {
            int numread = read4(buf);
            if (numread >= n) {
                result += n;
                return result;
            }
            if (numread < 4) {
                result += numread;
                break;
            }
            n -= numread;
            result += numread;
            buf += numread;
        }
        return result;
    }
};

再说2

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 may be called multiple times.

2里面说要call multiple times。这里面的问题就是,文件的cursor是隐藏的,所以我们看不到。一次读过就回不去了,所以我们要把暂时用不到,但是读了的都放入buff里面。下面是代码

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

class Solution {
private:
    list<char> buff;
public:
    int read(char *buf, int n) {
        if (n <= 0)
            return 0;
        if (!buff.empty()) {
            int idx = 0;
            while (n > 0 && !buff.empty()) {
                *buf = buff.front();
                buff.pop_front();
                idx++;
                n--;
                buf++;
            }
            return read(buf,n)+idx;
        }
        
        int result = 0;
        while (n > 0) {
            int numread = read4(buf);
            if (numread >= n) {
                result += n;
                for (int i = 0; i < numread-n; i++) {
                    buff.push_back(*(buf+n+i));
                }
                return result;
            }
            if (numread < 4) {
                result += numread;
                break;
            }
            n -= numread;
            result += numread;
            buf += numread;
        }
        return result;
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值