[LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

简介:

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.

这道题是之前那道Read N Characters Given Read4的拓展,那道题说read函数只能调用一次,而这道题说read函数可以调用多次,那么难度就增加了,为了更简单直观的说明问题,我们举个简单的例子吧,比如:

buf = "ab", [read(1),read(2)],返回 ["a","b"]

那么第一次调用read(1)后,从buf中读出一个字符,那么就是第一个字符a,然后又调用了一个read(2),想取出两个字符,但是buf中只剩一个b了,所以就把取出的结果就是b。再来看一个例子:

buf = "a", [read(0),read(1),read(2)],返回 ["","a",""]

第一次调用read(0),不取任何字符,返回空,第二次调用read(1),取一个字符,buf中只有一个字符,取出为a,然后再调用read(2),想取出两个字符,但是buf中没有字符了,所以取出为空。

但是这道题我不太懂的地方是明明函数返回的是int类型啊,为啥OJ的output都是vector<char>类的,然后我就在网上找了下面两种能通过OJ的解法,大概看了看,也是看的个一知半解,貌似是用两个变量readPos和writePos来记录读取和写的位置,i从0到n开始循环,如果此时读和写的位置相同,那么我们调用read4函数,将结果赋给writePos,把readPos置零,如果writePos为零的话,说明buf中没有东西了,返回当前的坐标i。然后我们用内置的buff变量的readPos位置覆盖输入字符串buf的i位置,如果完成遍历,返回n,参见代码如下:

解法一:

class Solution {
public:
    int read(char *buf, int n) {
        for (int i = 0; i < n; ++i) {
            if (readPos == writePos) {
                writePos = read4(buff);
                readPos = 0;
                if (writePos == 0) return i;
            }
            buf[i] = buff[readPos++];
        }
        return n;
    }
private:
    int readPos = 0, writePos = 0;
    char buff[4];
};

下面这种方法和上面的方法基本相同,稍稍改变了些解法,使得看起来更加简洁一些:

解法二:

class Solution {
public:
    int read(char *buf, int n) {
        int i = 0;
        while (i < n && (readPos < writePos || (readPos = 0) < (writePos = read4(buff))))
            buf[i++] = buff[readPos++];
        return i;
    }
    char buff[4];
    int readPos = 0, writePos = 0;
};

本文转自博客园Grandyang的博客,原文链接:用Read4来读取N个字符之二 - 多次调用[LeetCode] Read N Characters Given Read4 II - Call multiple times ,如需转载请自行联系原博主。

相关文章
|
3月前
|
存储 算法 程序员
【Leetcode 程序员面试金典 01.01】判定字符是否唯一 —— 位运算|哈希表
可以使用哈希表或位运算来解决此问题:由题可知s[i]仅包含小写字母,int[26]即能表示字符的出现次数;
|
3月前
|
算法
LeetCode算法题---无重复字符的最长子串、寻找两个正序数组的中位数(三)
LeetCode算法题---无重复字符的最长子串、寻找两个正序数组的中位数(三)
37 0
|
1月前
|
网络协议
《 LeetCode 热题 HOT 100》——无重复字符的最长子串
《 LeetCode 热题 HOT 100》——无重复字符的最长子串
|
2月前
|
存储 算法 Go
LeetCode 第三题: 无重复字符的最长子串
  给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
|
3月前
【Leetcode 2707】字符串中的额外字符 —— 动态规划
1. 状态定义:把`s[i−1]`当做是额外字符,`d[i] = d[i−1] + 1` 2. 状态转移方程:遍历所有的`j(j∈[0,i−1])`,如果子字符串`s[j...i−1]`存在于`dictionary`中,那么`d[i] = min d[j] 3. 初始状态`d[0] = 0`,最终答案为`d[n]`
|
3月前
leetcode:3. 无重复字符的最长子串
leetcode:3. 无重复字符的最长子串
16 0
|
3月前
|
算法
leetcode:387. 字符串中的第一个唯一字符
leetcode:387. 字符串中的第一个唯一字符
12 0
|
3月前
|
存储 索引
力扣1445 连续字符
力扣1445 连续字符
21 0
|
3月前
leetcode-777:在LR字符串中交换相邻字符
leetcode-777:在LR字符串中交换相邻字符
23 0