leetcode read4

/*给你一个文件,并且该文件只能通过给定的 read4 方法来读取,请实现一个方法使其能够读取 n 个字符。

read4 方法:

API read4 可以从文件中读取 4 个连续的字符,并且将它们写入缓存数组 buf 中。

返回值为实际读取的字符个数。

注意 read4() 自身拥有文件指针,很类似于 C 语言中的 FILE *fp 。

read4 的定义:

参数类型: char[] buf
返回类型: int

注意: buf[] 是目标缓存区不是源缓存区,read4 的返回结果将会复制到 buf[] 当中。
下列是一些使用 read4 的例子:

File file(“abcdefghijk”); // 文件名为 “abcdefghijk”, 初始文件指针 (fp) 指向 ‘a’
char[] buf = new char[4]; // 创建一个缓存区使其能容纳足够的字符
read4(buf); // read4 返回 4。现在 buf = “abcd”,fp 指向 ‘e’
read4(buf); // read4 返回 4。现在 buf = “efgh”,fp 指向 ‘i’
read4(buf); // read4 返回 3。现在 buf = “ijk”,fp 指向文件末尾
read 方法:

通过使用 read4 方法,实现 read 方法。该方法可以从文件中读取 n 个字符并将其存储到缓存数组 buf 中。您 不能 直接操作文件。

返回值为实际读取的字符。

read 的定义:

参数类型: char[] buf, int n
返回类型: int

注意: buf[] 是目标缓存区不是源缓存区,你需要将结果写入 buf[] 中。

示例 1:

输入: file = “abc”, n = 4
输出: 3
解释: 当执行你的 rand 方法后,buf 需要包含 “abc”。 文件一共 3 个字符,因此返回 3。 注意 “abc” 是文件的内容,不是 buf 的内容,buf 是你需要写入结果的目标缓存区。
示例 2:

输入: file = “abcde”, n = 5
输出: 5
解释: 当执行你的 rand 方法后,buf 需要包含 “abcde”。文件共 5 个字符,因此返回 5。
示例 3:

输入: file = “abcdABCD1234”, n = 12
输出: 12
解释: 当执行你的 rand 方法后,buf 需要包含 “abcdABCD1234”。文件一共 12 个字符,因此返回 12。
示例 4:

输入: file = “leetcode”, n = 5
输出: 5
解释: 当执行你的 rand 方法后,buf 需要包含 “leetc”。文件中一共 5 个字符,因此返回 5。

注意:

你 不能 直接操作该文件,文件只能通过 read4 获取而 不能 通过 read。
read 函数只在每个测试用例调用一次。
你可以假定目标缓存数组 buf 保证有足够的空间存下 n 个字符

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/read-n-characters-given-read4
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
————————————————
版权声明:本文为CSDN博主「15130140362」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liu_12345_liu/article/details/102005980*/
class Reader4 {
    
    
    public int read4(char[] buf) {
    
    
        return 0;
    }
}

public class read4 extends Reader4 {
    
    
    /**
     * @param buf Destination buffer
     * @param n   Number of characters to read
     * @return The number of actual characters read
     */
    public int read(char[] buf, int n) {
    
    
        int sum = 0;
        char[] temp = new char[4];

        int index = 0;

        while (index < n) {
    
    
            int m = read4(temp);
            for (int i = 0; i < m && index + i < n; i++) {
    
    
                buf[index + i] = temp[i];
            }
            sum += m;
            index += 4;  // 因为有可能n大于总的字符数量,所以在这里我们需要每次加4,而不能加每次实际独到的字符,以避免死循环
        }
        return sum > n ? n : sum; // 有可能我们读5个字符,假设文本中有8个字符,我们实际上读取了8个,但是我们需要返回5。如果文本中只有8个但是n为10表示要读取10个,我们只能返回8因为实际读取只读取到了8个
    }
}

猜你喜欢

转载自blog.csdn.net/liu_12345_liu/article/details/102472290
今日推荐