[Leetcode] 157. Read N Characters Given Read4 解题报告

leetcode157java-Leetcode:力码 leetcode 157 java leetcode 使用 java,c++ 的解决方案 这是我的 leetcode 解决方案文件夹。 语言:java,c++ 最后更新:2017-06-16 我已经解决了 238/547 个问题(101 个问题是收费的)。 源代码是使用该工具获取的。 # 问题 解决方案 困难 001 简单的 002 中等的 003 中等的 005 中等的 006 中等的 007 简单的 008 中等的 009 简单的 010 难的 011 中等的 012 中等的 013 简单的 014 简单的 015 中等的 017 中等的 018 中等的 019 中等的 020 简单的 021 简单的 022 中等的 023 难的 024 中等的 025 难的 026 简单的 027 简单的 028 简单的 035 简单的 036 中等的 040 中等的 041 难的 042 难的 043 中等的 046 中等的 048 中等的 049 中等的 050 中等的 053 简单的 058 简单的 061 中等的 062 中等的 064 中等的 066 简单的 067 简单的 069 简 立即下载

题目:

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 > 0),那么就调用API函数进行读取。读取之后需要判断返回值,一旦发现返回值为0,说明文件中的内容已经被全部读取,则提前返回。否则就需要判断返回值和剩余需要读取的字符的大小,根据比较的结果向buffer中复制读取的内容,并且更新n的值。

代码:

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

class Solution {
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) {
        int ret = 0;
        char temp[4];
        while(n > 0) {                          // still have content to read
            int num = read4(temp);
            if(num == 0) {                      // nothing left to read
                break;
            }
            if(n > num) {                       // put all the content to buffer
                for(int i = 0; i < num; ++i) {
                    buf[ret++] = temp[i];
                }
                n -= num;
            }
            else {                              // only partial contents are left to put into the buffer
                for(int i = 0; i < n; ++i) {
                    buf[ret++] = temp[i];
                }
                n = 0;
            }
        }
        return ret;
    }
};
数电实训:万年历 万年历 阅读详情

相关推荐

FPGA远程更新实战:Multiboot双镜像配置与Flash分区策略

本文详细介绍了FPGA远程更新中Multiboot双镜像的配置与实现,重点对比了XDC约束和ICAPE2原语两种配置方案的优缺点及适用场景。提供了基于W25Q128 Flash的非对称分区策略、MCS文件生成与烧写方法,并深入探讨了远程更新的流程容错处理与可靠性优化建议,帮助开发者构建稳定可靠的FPGA远程更新系统。

ss78901的博客 851

【LeetCode - 157】用 Read4 读取 N 个字符

【简单】字符串

学哥斌的博客 858

地震勘探中的频谱分析:从基础到应用

本文深入探讨了地震勘探中的频谱分析技术,从基础原理到实际应用。通过傅里叶变换等工具,频谱分析能有效识别不同地震波的频率特征,指导数据采集和处理。文章结合油田勘探案例,展示了如何利用振幅谱和相位谱提升信噪比,并介绍了智能滤波等前沿技术在地震数据处理中的应用。

weixin_42557830的博客 416

【Leetcode】157. Read N Characters Given Read4

题目地址: https://leetcode.com/problems/read-n-characters-given-read4/ 给定一个API,函数是这样的:int read4(char[] buf4),其能在某个文件里顺次读最多444个字符(每次读取之后指针都会后移到下一次应该开始读取的位置),如果还没读到文件尾就已经读了444个字符了,则就读到文件尾,读到的字符会填进长度为444的数组buf4中,其返回的是读到的字符个数。要求用这个API实现读nnn个字符的操作,返回实际读到的字符个数,并且将读入

数学、算法爱好者的博客 348

基于STM32与FPGA Cyclone IV的FSMC高速数据通信实战解析

本文详细解析了基于STM32的FSMC接口与FPGA Cyclone IV实现高速并行数据通信的实战方案。通过将FPGA模拟为SRAM设备,构建了MCU与FPGA之间的高速数据通道,并深入探讨了硬件连接、STM32软件配置、FPGA逻辑设计及性能优化等关键步骤,为图像处理、高速采集等需要大数据量交换的嵌入式应用提供了稳定高效的通信解决方案。

swift5iosmith的博客 301

UniTouch:多模态触觉对齐技术如何重塑零样本跨模态应用

UniTouch模型通过创新的多模态触觉对齐技术,成功将触觉数据映射到通用的视觉-语言嵌入空间,解决了触觉模态的“孤岛困境”。该技术利用对比学习和传感器特定标记,实现了高质量的零样本学习能力,使模型无需额外训练即可完成触觉分类、描述等任务,并能驱动跨模态生成,为机器人、VR和智能制造等领域提供了强大的触觉理解新范式。

weixin_29044713的博客 415

java read(buf),【LeetCode(Java) - 157】用 Read4 读取 N 个字符

【LeetCode(Java) - 157】用 Read4 读取 N 个字符【LeetCode(Java) - 157】用 Read4 读取 N 个字符文章目录1、题目描述2、解题思路3、解题代码1、题目描述2、解题思路??定义一个变量 tmp 表示调用 read4 读到的有效长度;??定义一个数组 bufTmp 用于装入 read4 读到的数据;??定义 length 表示已经 bufTmp 的...

weixin_35852816的博客 255

Far planner 代码系列(35)动态障碍物预处理_激光雷达部分

主要讲解动态障碍物在激光雷达部分的剔除机制

博客 1062

Leetcode 周赛157,5213,5214,5215,5216JAVA题解

Leetcode 周赛157,5213-5216JAVA题解 5213 只需统计奇数项和偶数项较少的一个即可 //5213 public int minCostToMoveChips(int[] chips) { int a=0;int b=0; for(int i=0;i<chips.length;i++) { ...

qq_43301939的博客 609

手把手教你 Ubuntu 24.04 离线安装 Ollama

本文介绍 Ubuntu 下离线安装 Ollama 并使用 Ollama 部署 llama3.1-70b 大模型

举世誉之而不加劝,举世非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。 1026

栅极驱动电路设计(一)

SiC驱动电路设计需满足更高标准,重点包括:驱动电压需18~20V开通、-3~-5V关断;栅极电阻优选5~10Ω以平衡开关性能与可靠性;必须集成欠压保护(阈值11~15V)和快速短路保护。多电平驱动控制通过动态调节栅压和驱动强度优化性能,如前期弱驱动抑制振荡,后期强驱动降低损耗。以NCE120ED65VTP4为例,栅压从18V提升至20V可使导通损耗降低15%。相比传统方案,有源驱动(含数字分段和模拟闭环控制)能更灵活地提升开关速度与效率。

嵌入式老牛的博客 461

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

leetcode 解题思路 6432

【LSTM回归预测】attention机制LSTM时间序列回归预测【含Matlab源码 1992期】

attention机制LSTM时间序列回归预测 完整的代码,方可运行;可提供运行操作视频!适合小白!

订阅付费专栏Matlab(奶茶价版),可赠送奶茶价版付费专栏指定代码1份; 2596

[leetcode] 157. Read N Characters Given Read4 解题报告

题目链接: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 re

小榕流光的专栏 3140

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【LeetCode】157. Read N Characters Given Read4 解题报告(C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法直接调用日期 题目地址:https://leetcode-cn.com/problems/read-n-characters-given-read4/ 题目描述 Given a file and assume that you can on...

负雪明烛 1343

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

【LeetCode】157. Read N Characters Given Read4

LeetCode157 传送门 解法: 创建新的buf_4,while循环判断结束,两个指针指向子串的首尾位置,将结果依次贴进buf。 Python源码: Runtime:20 ms, faster than42.13%ofPythononline submissions forRead N Characters Given Read4. Memory Usage:...

weixin_42344158的博客 251

LeetCode 157. Read N Characters Given Read4

本文转载自 : https://blog.csdn.net/xinqrs01/article/details/54093704 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 e...

Umbrella Corporation 9559

leetcode 157: Read N Characters Given Read4

Read N Characters Given Read4 Total Accepted: 960 Total Submissions: 3775 The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of char

西施豆腐渣 4965

[LeetCode] 157. Read N Characters Given Read4

原题链接:https://leetcode.com/problems/read-n-characters-given-read4/ 这也是一道带锁题,题目的意思是给定一个函数read4(char[] buf) 的函数,每次可以最多读4个字节的字符,然后要求通过给定的函数来实现一个函数,能按照要求读出n个字符。 这道题目是http://foolmancode.blogspot.com/2016/

foolman 1481

[LeetCode] 157&158. Read N Characters Given Read4 + call multiple times

Problem: https://leetcode.com/problems/read-n-characters-given-read4/ Solution for call one time: read4读4个 -> 4个一组放入buf中 -> read4继续读 -> 直到读满n个或者字符串已经读完(不满n个) public class Solution extends Rea...

Renoler的博客 155

Leetcode 157 Read N Characters Given Read4

思路:这题很奇怪,返回值是int,但是却要输出buf才行(不然就报错)。题目也很难理解。建议回炉重造。复习到这题时候,建议直接回leetcode discussion部分去看别人解释题目意思。我看题目明明说只能call一次read4函数,但是大家答案中都有循环,都多次调用了,这样可以吗,我也不太确定。但是看到大家都这样做,我也这样做了。理解题目意思之后也比较简单吧。没啥好说的。直接放代码好吧: /** * The read4 API is defined in the parent class Re...

GoodJobJasper的博客 196

LeetCode C++ 157. Read N Characters Given Read4【String/Design】简单

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 a

memcpy0的博客 472

leetcode[157] Read N Characters Given Read4

题目意思是给你一个read4的函数,实现一个readn的函数。 一开始题目一直没搞懂,原来,read4(buf) 是指,读4个字符存到buf,或者读剩下的不足四个的字符,返回的数字是存到buf里的字符数。 没有买书,只能网上看看别人分享的代码: // Forward declaration of the read4 API. int read4(char *buf); ...

161

LeetCode #157 - 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 i...

LawFile的博客 188

LeetCode 157. Read N Characters Given Read4(读文件流)

原题网址: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 re

James Pan 683
上一篇: [Leetcode] 156. Binary Tree Upside Down 解题报告
下一篇: [Leetcode] 158. Read N Characters Given Read4 II - Call multiple times 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值