157. Read N Characters Given Read4

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 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 API来实现一个可以读取任意长度n个字符的read方法,如果buf中的字符数小于n则返回buf的字符数。

一直调用read4,直到count大于n了(buf > n)或者read4的返回值不为4了(buf < n)。方法的返回值为n和读出来的count中较小的那一个。

代码
/* The read4 API is defined in the parent class Reader4.
      int read4(char[] buf); */

public class Solution extends Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Maximum number of characters to read
     * @return    The number of characters read
     */
    private int prev = 4; 
    private int count = 0;
    private char[] readChars = new char[4];
    /*
    Basically, call multiple times means, you want to resume where you left off last time.
    That means, if there are chars left in readChars (from the Read4 from the previous call of ReadN), you want to transfer them into buffer first.
    */
    public int read(char[] buf, int n) {
        int total = 0;
        
        /** 
        EdgeCases:
        If N < 4, do you transfer all the remaining chars from previous call or just N?
        what if there are still characters left in readChars after transfering N to the buffer? In that case, you still want to    pick up from there next time.
        
        **/
        while(total < n){
            while(prev < count && total < n){
                buf[total] = readChars[prev];
                prev++;
                total++;
            }
            
            if(total < n){
                count = read4(readChars);
                prev = 0;

                //You only want to read till N even if there are more chars returned by read4.
                int toRead = Math.min(count, n-total);
                
                while(prev < toRead){
                    buf[total] = readChars[prev];
                    prev++;
                    total++;
                }
                
                //Done. There are no more chars to read from Read4.
                if(count < 4) break;
            }
        }
        
        return total;
    }
}
// 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 temp;
        int count = 0;
        do{
            temp = read4(buf + count);
            count += temp;
        }while(temp == 4 && count < n);
        return min(count,n);
    }
};

时间复杂度:未知


反思

对于指针的理解不到位,在内存中的位移可以带到下一个内存的位置上去。

贝叶斯时空模型-INLA-4 该文章中,会简化数学计算的过程,主要是针对,在有数据的基础上,如何应用贝叶斯时空模型,找出影响因素,绘制时间变化的空间分布预测图。上述中关键一步与前几期介绍的不一样的地方是,我们加入了n.group参数,用来指定年份,先看一下Year有多少年的,然后封装在s.index里面。在前述的内容中,我们介绍了,如何处理空间的数据,利用海拔高度预测降雨量的例子。建立好数据及mesh以后,我们现在写INLA公式,如果不考虑残差项,那就是固定效应,现在我们增加残差项,及空间与时间效应。这里的Mesh设定值为10,20。 阅读详情

相关推荐

《统计学习方法》——李航 学习大纲

最近在学习李航写的统计学习方法概论, 每一章都用xmind理清了思路,括号里是书里的公式,第一次写博文,敬请指教~~~~ 第一章 统计学习方法论 第二章 感知机 每个方法其实只需要着重掌握三要素和输入输出就可以了,主要看模型、策略和算法。 感知机主要是二类分类的线性分类模型。 看到后面会注意感知机和支持向量机的区别。 第三章 k近

wjy619251334的博客 1万+

157. Read N Characters Given Read4读取给定Read4的N个字符Python

有一个read4方法每次只能读4个字符,它有一个buf[]缓存它读的内容。要自己写一个read方法,输出读了多少个字符。 给定一个文件并假定您只能使用给定的方法读取该文件read4,请实现一个方法以读取n个字符。 说明read4: APIread4从文件中读取4个连续字符,然后将这些字符写入缓冲区数组buf。 返回值是读取的实际字符数。 请注意,它read4()具有自己的文件指针,就像FILE *fp在C语言中一样。 read4的定义: 参数:char [] buf ...

weixin_44516745的博客 279

告别Failed to start错误!保姆级教程:用Ubuntu20.04安装盘搞定GRUB与系统服务修复

本文提供Ubuntu20.04系统启动失败的专业修复指南,详细讲解如何利用安装盘解决GRUB与系统服务问题,避免重装系统导致的数据丢失。涵盖分区挂载、chroot环境操作、服务诊断与修复等实用技巧,帮助用户高效恢复系统正常运行。

weixin_32578161的博客 338

LeetCode 题解(233) : 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

532

【软件逆向学习记录】x64dbg调试器入门2:主调试窗口功能及字符串搜索功能介绍

在x64dbg中, ​​CPU视图 是调试过程中最核心的界面,通常被称为​​主调试窗口​​或​​调试视图​​,总体分为四个区域,代码区(反汇编),数据区(内存窗口),寄存器区和堆栈区,详细位置如下图:示例数据代码区最左侧为内存地址,第二块区域为程序代码值,原始程序代码经编译和汇编后变为二进制机器码存储在计算机中,调试器将其展示为易于阅读的十六进制,第三块区域是机器码反汇编后的汇编代码,最右侧为代码的注释窗口,有一些原始的注释,也可以自己添加注释PS:汇编和反汇编解释。

tusikong的博客 2927

VideoPose3D | 环境搭建

文章目录环境要求下载PyTorch基本步骤验证安装问题修复数据集设置Human3.6MCPN detections配置wget命令安装CPN评估环境测试问题测试报错 无法识别参数3 3 3 3卡在最后评估 主要参考:GitHub / facebookresearch / VideoPose3D 环境要求 Dependencies Make sure you have the followin...

D0 8662

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

9.27华为OD机试真题 新系统 - 均衡调度 (Java/Py/C/C++/Js/Go)

9.26华为OD机试真题 新系统 - 均衡调度 (Java/Py/C/C++/Js/Go)

算法大师! 571

leetcode 算法题157 (简单037) 用 Read4 读取 N 个字符

leetcode 算法题157 (简单037) 用 Read4 读取 N 个字符 题目介绍 会员试题 先写非会员的 等写完了 开个会员统一写会员题目....

FYuu95100的博客 881

深入浅出:大型语言模型(LLM)的全面解读

在当今的数字化时代,人工智能技术已经成为各行各业的核心驱动力之一。而在人工智能的领域中,大型语言模型(LLM)作为一种引人瞩目的技术,正在以其强大的语言理解和生成能力引领着一场革命。本文旨在深入浅出地解读大型语言模型,探讨其背后的原理、技术突破、应用场景以及未来发展趋势。

fudaihb的博客 10万+

【R-terra包|第一节】- 栅格相关性、散点图 、波段的计算与异常值处理

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结 —```mermaid flowchat st=>start: 开始 e=>end: 结束 op=>operation: 我的操作 cond=>condition: 确认? st->op->cond cond(yes)->e cond(no)->op 前言 提示:这里可以添加本文要记录的大概内容: 例如:随着人工智能.

地理空间科学的博客 5617

leetcode:157. 用 Read4 读取 N 个字符(read只调用一次)

Read4 函数,每次可以从一个文件中最多读出4个字符,如果文件中的字符不足4个字符时,返回准确的当前剩余的字符数。给你一个文件,并且该文件只能通过给定的 read4 方法来读取,请实现一个方法使其能够读取 n 个字符。API read4 可以从文件中读取 4 个连续的字符,并且将它们写入缓存数组 buf 中。注意 read4() 自身拥有文件指针,很类似于 C 语言中的 FILE *fp。

OceanStar的博客 277

Android 10.0 SystemUI 屏蔽状态栏消息推送弹出的悬浮通知

在状态栏中,有一些通知类似 消息推送 弹出的 悬浮通知,弹出30秒之后会消失掉 要想屏蔽掉这些通知,首选知道这些通知是怎么产生的 通知类型 NotificationContentInflater.java 详细的介绍各种通知的类型 frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/notification/row/NotificationContentInflater.java /** * The defau

安卓兼职framework和app工程师的博客 1545

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

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

【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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[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

[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 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

夜色之浓,莫过于黎明前的黑暗。 371

[leetcode 157] Read N Characters Given Read4

Question: 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 charact

清空 1453

【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

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
上一篇: 155. Min Stack
下一篇: 160. Intersection of Two Linked Lists
BigFatSheep
博客等级 码龄10年 16粉丝 393原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值