C++实现LeetCode(157.用Read4来读取N个字符)

 更新时间:2021年07月30日 14:37:42   作者:Grandyang  
这篇文章主要介绍了C++实现LeetCode(157.用Read4来读取N个字符),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

[LeetCode] 157. Read N Characters Given Read4 用Read4来读取N个字符

Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters.

Method read4:

The API read4 reads 4 consecutive characters from the file, then writes those characters into the buffer array buf.

The return value is the number of actual characters read.

Note that read4() has its own file pointer, much like FILE *fp in C.

Definition of read4:

    Parameter:  char[] buf
Returns:    int

Note: buf[] is destination not source, the results from read4 will be copied to buf[]

Below is a high level example of how read4 works:

File file("abcdefghijk"); // File is "abcdefghijk", initially file pointer (fp) points to 'a'
char[] buf = new char[4]; // Create buffer with enough space to store characters
read4(buf); // read4 returns 4. Now buf = "abcd", fp points to 'e'
read4(buf); // read4 returns 4. Now buf = "efgh", fp points to 'i'
read4(buf); // read4 returns 3. Now buf = "ijk", fp points to end of file

Method read:

By using the read4 method, implement the method read that reads n characters from the file and store it in the buffer array buf. Consider that you cannot manipulate the file directly.

The return value is the number of actual characters read.

Definition of read:

    Parameters: char[] buf, int n
Returns: int

Note: buf[] is destination not source, you will need to write the results to buf[]

Example 1:

Input: file = "abc", n = 4
Output: 3
Explanation: After calling your read method, buf should contain "abc". We read a total of 3 characters from the file, so return 3. Note that "abc" is the file's content, not buf. buf is the destination buffer that you will have to write the results to.

Example 2:

Input: file = "abcde", n = 5
Output: 5
Explanation: After calling your read method, buf should contain "abcde". We read a total of 5 characters from the file, so return 5.

Example 3:

Input: file = "abcdABCD1234", n = 12
Output: 12
Explanation: After calling your read method, buf should contain "abcdABCD1234". We read a total of 12 characters from the file, so return 12.

Example 4:

Input: file = "leetcode", n = 5
Output: 5
Explanation: After calling your read method, buf should contain "leetc". We read a total of 5 characters from the file, so return 5.

Note:

  1. Consider that you cannot manipulate the file directly, the file is only accesible for read4 but not for read.
  2. The read function will only be called once for each test case.
  3. You may assume the destination buffer array, buf, is guaranteed to have enough space for storing n characters.

这道题给了我们一个 Read4 函数,每次可以从一个文件中最多读出4个字符,如果文件中的字符不足4个字符时,返回准确的当前剩余的字符数。现在让实现一个最多能读取n个字符的函数。这题有迭代和递归的两种解法,先来看迭代的方法,思路是每4个读一次,然后把读出的结果判断一下,如果为0的话,说明此时的 buf 已经被读完,跳出循环,直接返回 res 和n之中的较小值。否则一直读入,直到读完n个字符,循环结束,最后再返回 res 和n之中的较小值,参见代码如下:

解法一:

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

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

下面来看递归的解法,这个也不难,对 buf 调用 read4 函数,然后判断返回值t,如果返回值t大于等于n,说明此时n不大于4,直接返回n即可,如果此返回值t小于4,直接返回t即可,如果都不是,则直接返回调用递归函数加上4,其中递归函数的 buf 应往后推4个字符,此时n变成n-4即可,参见代码如下:

解法二:

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

class Solution {
public:
    int read(char *buf, int n) {
        int t = read4(buf);
        if (t >= n) return n;
        if (t < 4) return t;
        return 4 + read(&buf[4], n - 4);
    }
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/157

类似题目:

Read N Characters Given Read4 II - Call multiple times

参考资料:

https://leetcode.com/problems/read-n-characters-given-read4/

https://leetcode.com/problems/read-n-characters-given-read4/discuss/49557/Accepted-clean-java-solution

https://leetcode.com/problems/read-n-characters-given-read4/discuss/49496/Another-accepted-Java-solution

到此这篇关于C++实现LeetCode(157.用Read4来读取N个字符)的文章就介绍到这了,更多相关C++实现用Read4来读取N个字符内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于c++ ege图形库实现五子棋游戏

    基于c++ ege图形库实现五子棋游戏

    这篇文章主要为大家详细介绍了基于c++ ege图形库实现五子棋游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Qt编译OpenCV的实现步骤

    Qt编译OpenCV的实现步骤

    本文主要介绍了Qt编译OpenCV的实现步骤,通过详细的步骤和说明,帮助开发者在Qt环境中成功集成并编译OpenCV,从而为各类计算机视觉项目提供强大的支持,感兴趣的可以了解一下
    2024-01-01
  • C++中const与#define的利弊分析

    C++中const与#define的利弊分析

    C++中不但可以用define定义常量还可以用const定义常量,下面这篇文章主要给大家分析介绍了关于C++中const与#define的利弊,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2018-05-05
  • C语言链表实现销售管理系统

    C语言链表实现销售管理系统

    这篇文章主要为大家详细介绍了C语言链表实现销售管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • c语言实现计算圆周率的近似值

    c语言实现计算圆周率的近似值

    这篇文章主要介绍了c语言实现计算圆周率的近似值方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 一文带你探索C++中类型转换的奥秘

    一文带你探索C++中类型转换的奥秘

    C++ 提供了四种类型转换方式,帮助我们在不同数据类型之间进行有效的数据传递和操作,这些类型转换方式在不同的场景下有各自的优势和适用性,下面我们就来深入了解一下吧
    2023-10-10
  • C语言实现时间戳转日期的算法(推荐)

    C语言实现时间戳转日期的算法(推荐)

    下面小编就为大家带来一篇C语言实现时间戳转日期的算法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • C语言中单链表(不带头结点)基本操作的实现详解

    C语言中单链表(不带头结点)基本操作的实现详解

    链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。本文主要和大家聊聊C语言中单链表(不带头结点)的基本操作,感兴趣的小伙伴可以了解一下
    2022-11-11
  • c++中的stack和dequeue解析

    c++中的stack和dequeue解析

    这篇文章主要介绍了c++中的stack和dequeue介绍,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-05-05
  • C语言进阶之字符串查找库函数详解

    C语言进阶之字符串查找库函数详解

    字符串是一种非常重要的数据类型,但是C语言不存在显式的字符串类型,C语言中的字符串都以字符串常量的形式出现或存储在字符数组中,下面这篇文章主要给大家介绍了关于C语言进阶之字符串查找库函数的相关资料,需要的朋友可以参考下
    2023-01-01

最新评论