打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
​LeetCode刷题实战157:用 Read4 读取 N 个字符
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 用 Read4 读取 N 个字符(这题Leetcode需要会员才能看),我们先来看题面:
https://leetcode-cn.com/problems/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 reads n
characters from the file.
Note: The read function will only be called once for each test case.

题意

给你一个文件,并且该文件只能通过给定的 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[] 中。


样例

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

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

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

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

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


解题


定义一个变量 tmp 表示调用 read4 读到的有效长度;

定义一个数组 bufTmp 用于装入 read4 读到的数据;

定义 length 表示已经 bufTmp 的数据有效长度;

1、循环调用 read4,直到读到的有效长度 tmp 为 0;

2、每次读取到的 read4 的有效数据装入 bufTmp 中,直到 length == n 为止(不包括 n ,因为索引从 0 开始)

public class Solution 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 tmp;
        int length = 0;
        char[] bufTmp = new char[4];
        while ((tmp = read4(bufTmp)) != 0) {
            for (int i = 0; i < tmp && length < n; i++) {
                buf[length] = bufTmp[i];
                length++;
            }
        }
        return length;
    }
}


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

上期推文:

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:上下翻转二叉树

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
浅谈无缓存I/O操作和标准I/O文件操作区别
进程通信-管道(lockf)
《UNIX环境高级编程》读书笔记6
java io --- Reader类
字符流相关函数
深入理解C语言的IO及缓冲操作
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服