​LeetCode刷题实战158:用 Read4 读取 N 个字符 II

本文介绍LeetCode上的“用Read4读取N个字符II”问题,讲解如何利用read4方法实现read方法,以读取指定数量的字符。通过具体示例,给出了一种可行的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 用 Read4 读取 N 个字符 II(这题Leetcode需要会员才能看),我们先来看题面:

https://leetcode-cn.com/problems/read-n-characters-given-read4-ii-call-multiple-times/

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.

题意

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

注意:你的 read 方法可能会被调用多次。

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 file("abc");

Solution sol;

// 假定 buf 已经被分配了内存,并且有足够的空间来存储文件中的所有字符。

sol.read(buf, 1); // 当调用了您的 read 方法后,buf 需要包含 "a"。一共读取 1 个字符,因此返回 1。

sol.read(buf, 2); // 现在 buf 需要包含 "bc"。一共读取 2 个字符,因此返回 2。

sol.read(buf, 1); // 由于已经到达了文件末尾,没有更多的字符可以读取,因此返回 0。


示例 2:

File file("abc");

Solution sol;

sol.read(buf, 4); // 当调用了您的 read 方法后,buf 需要包含 "abc"。一共只能读取 3 个字符,因此返回 3。

sol.read(buf, 1); // 由于已经到达了文件末尾,没有更多的字符可以读取,因此返回 0。

解题

https://blog.youkuaiyun.com/qq_29051413/article/details/108559764

用一个容量为 4 的数组 fileBuf 存放最近一次 read4 读取到的数据。

用一个变量 readOffset 指向 fileBuf 尚未使用的开头数据索引。

用一个变量 bufSize 表示最近一次 read4 读取到的数据字符个数。

大致算法逻辑如下:

每次调用 read(char[] buf, int n) 时,都是调用 n 次 getNextCharFromFile()。

 getNextCharFromFile() 逻辑如下:

当 readOffset < bufSize,说明最近一次读取到 fileBuf 的数据还没用完,从 fileBuf 中取出一个字符,然后 readOffset++;

当 readOffset == bufSize,说明最近一次 read4 读到的数据已经用完了,于是再调用一次 read4,重置 readOffset 为 0,bufSize = read4(fileBuf)。接着从 fileBuf 拿出一个字符返回出去,readOffset++。

public class Solution extends Reader4 {
   
    private char[] fileBuf = new char[4]; // read4 读到的数据存到这里
    private int readOffset = 0; // 指向 fileBuf 尚未使用的开头索引
    private int bufSize = 0; // 最近一次从 read4 读到的数据有多少个

    public int read(char[] buf, int n) {
        for (int i = 0; i < n; i++) {
            char nextChar = getNextCharFromFile();
            if (nextChar == 0) {
                return i;
            } else {
                buf[i] = nextChar;
            }
        }
        return n;
    }

    public char getNextCharFromFile() {
        // 比如最近一次 read4 读到的有效数据有 2 个,此时 readOffset == 2,说明 fileBuf 已经没有未使用的数据,需要重新 read4
        if (readOffset == bufSize) {
            bufSize = read4(fileBuf);
            readOffset = 0;
            if (bufSize == 0) {
                return 0;
            }
        }
        // readOffset 永远指向尚未读取的数据的最开头
        return fileBuf[readOffset++];
    }
}

好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力。

上期推文:

LeetCode1-140题汇总,希望对你有点帮助!

LeetCode刷题实战141:环形链表

LeetCode刷题实战142:环形链表 II

LeetCode刷题实战143:重排链表

LeetCode刷题实战144:二叉树的前序遍历

LeetCode刷题实战145:二叉树的后序遍历

LeetCode刷题实战146:LRU 缓存机制

LeetCode刷题实战147:对链表进行插入排序

LeetCode刷题实战148:排序链表

LeetCode刷题实战149:直线上最多的点数

LeetCode刷题实战150:逆波兰表达式求值

LeetCode刷题实战151:翻转字符串里的单词

LeetCode刷题实战152:乘积最大子数组

LeetCode刷题实战153:寻找旋转排序数组中的最小值

LeetCode刷题实战154:寻找旋转排序数组中的最小值 II

LeetCode刷题实战155:最小栈

LeetCode刷题实战156:上下翻转二叉树

LeetCode刷题实战157:用 Read4 读取 N 个字符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值