LeetCode - Read N Characters Given Read4 I && II

本文介绍LeetCode上的两道关于文件读取的题目。第一题要求实现读取n个字符的功能,考虑到n可能不是4的倍数及文件大小可能小于n的情况。第二题则增加了缓存未完全使用的字符到buffer的需求,以便下次读取时先使用。

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

Read N Characters Given Read4 I 

https://leetcode.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.


这道题还是比较简单的,唯一需要注意的就是n不是4的倍数的情况,还有就是被读的文件size小于n的情况,在下面的代码中用eof表示文件已经读完了。

public class Solution extends Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Maximum number of characters to read
     * @return    The number of characters read
     */
    public int read(char[] buf, int n) {
        char[] buffer = new char[4];
        int readBytes = 0;
        boolean eof = false;
        while(!eof && readBytes<n){
            int sz = read4(buffer);
            if(sz < 4) eof = true;
            int bytes = Math.min(n - readBytes, sz);
            System.arraycopy(buffer, 0, buf, readBytes, bytes);
            readBytes += bytes;
        }
        return readBytes;
    }
}

还有就是注意System.arraycopy()函数的用法。

Read N Characters Given Read4 II

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

这道题跟上一道题不同的就是,当前面read4没用完的字符需要存在buffer里面,然后下一次调用这个函数的时候,要先把buffer里面的东西读出来,再继续读文件。

public class Solution extends Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Maximum number of characters to read
     * @return    The number of characters read
     */
    private char[] buffer = new char[4];
    private int offset = 0;
    private int bufsize = 0;
    public int read(char[] buf, int n) {
        int readBytes = 0;
        boolean eof = false;
        while(!eof && readBytes<n){
            int sz = bufsize>0 ? bufsize : read4(buffer);
            if(bufsize==0 && sz < 4) eof = true;
            int bytes = Math.min(n-readBytes, sz);
            System.arraycopy(buffer, offset, buf, readBytes, bytes);
            bufsize = sz - bytes;
            offset = (offset+bytes)%4;
            readBytes+=bytes;
        }
        return readBytes;
    }
}


增加第二题的C++版本:

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

class Solution {
private:
    char buffer[4];
    int offset = 0;
    int bufsize = 0;
public:
    /**
     * @param buf Destination buffer
     * @param n   Maximum number of characters to read
     * @return    The number of characters read
     */
    int read(char *buf, int n) {
        bool eof = false;
        int readBytes = 0;
        while(!eof && readBytes<n){
            int sz = bufsize>0?bufsize:read4(buffer); 
            if(bufsize==0 && sz<4) eof = true;
            int bytes = std::min(sz, n-readBytes);
            memcpy(buf+readBytes, buffer+offset, bytes);
            readBytes+=bytes;
            offset = (offset+bytes)%4;
            bufsize = sz-bytes;
        }
        return readBytes;
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值