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

银河麒麟V10 SP3下NFS客户端挂载失败?5个常见问题排查与修复 本文针对银河麒麟V10 SP3系统中NFS客户端挂载失败的常见问题,提供了5个实战排查方案。从基础服务状态检查到防火墙配置、版本兼容性、/etc/fstab设置及权限映射,详细解析了问题根源与解决方案,帮助用户快速恢复NFS文件共享功能。 阅读详情

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 Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Number of characters to read
     * @return    The number of actual characters read
     */
    public int read(char[] buf, int n) {
        char[] temp = new char[4];
        int index = 0, pointer = 0;
        while(index < n) {
        	// read4读四个
            int len = read4(temp);
            pointer = 0;
            // index没有读满n个 && read4中读出来的四个为一组
            while(index < n && pointer < len) {
                buf[index] = temp[pointer];
                pointer++;
                index++;
            }
            // 说明字符串已经被读完
            if(len < 4) break;
        }
        return index; // 存在index比n小的情况
    }
}

Solution for call multiple times:
改进的点在于,并不是每次都要把temp的pointer清空,而是只有在read4内所有的字符都被read调用后,再清空pointer

/**
 * 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   Number of characters to read
     * @return    The number of actual characters read
     */
    char[] temp = new char[4];
    int pointer = 0;
    int len = 0;
    public int read(char[] buf, int n) {
        int index = 0;
        while(index < n) {
            if(pointer == 0) { // 妙点:已经读完上一个temp
                len = read4(temp);
            }
            if(len == 0) break;
            while(index < n && pointer < len) {
                buf[index] = temp[pointer];
                index++;
                pointer++;
            }
            if(pointer >= len) { // 表示pointer已经到达temp的尾部
                pointer = 0;
            }
        }
        return index;
    }
}

Reference:https://www.youtube.com/watch?v=w3ke3MQTEJ8

《派出你的AI同事:WorkBuddy案例实战》062:投研资讯摘要与预警 投研资讯海量且时效性强,传统人工处理效率低、易遗漏。本文提出“投研资讯助理”四步法:多源解析(提取主体、事件、影响、时效)→ 摘要生成(三句话+关键数据)→ 预警分级(紧急/重要/关注+置信度)→ 分流推送。AI 7×24 小时值守,日均耗时从2小时降至10分钟,误报率显著降低。核心在于“解析准、摘要全、预警稳”,边界清晰:只提资讯与影响提示,不越权决策。建议分阶段上线,以误报归因持续优化规则,实现从“刷资讯”到“做决策”的跃迁。 阅读详情

相关推荐

基于光电传感的非接触式酒精浓度检测系统设计与实现

本文详细介绍了基于光电传感技术的非接触式酒精浓度检测系统设计与实现。系统利用940nm红外LED和OPT101光电传感器,结合朗伯-比尔定律,通过STM32微控制器进行信号采集与处理,实现了快速、准确的酒精浓度测量,测量精度达±1%,适用于工业检测和便携式应用场景。

y7z8a的博客 375

Leetcode 157,158, Read N Characters Given Read4

public int read(char[] buf, int n) { char[] buffer = new char[4]; int total = 0; boolean eof = false; //end of file while(!eof && total < n){ int coun

lstrue的专栏 906

基于ROS下的安卓手机图像和IMU跑ORB-SLAM3

接之前文章继续,手上没有现成的单目+IMU硬件,但是安卓手机几乎人人都有。因此,本文讲下如何用手机跑ORB-SLAM3的Mono和Mono_Inertial。 1. 手机与PC通信 基于ROS下的信息发布和订阅,手机和PC在一个局域网下进行信息(image和IMU)传输。 手机APK参考了github上的2个开源Android_Camera-IMU和android_ros_sensors,基于ros_java生成安卓APP,下载安装任一个APP到安卓手机,界面分别是这样: 手机和PC连一个无

h_372101的博客 1万+

LeetCode 157,158. Read N Characters Given Read4 I+II

Read N Characters Given Read4 这个题目和C里面读文件很像。 read4每次只能读四个字符,把字符放到buf里,返回值是实际读到的字符数。 而我们要做的是用read4实现一个能够读n个字符的函数 read,同样,读到的字符放到buf里。 if (count==0) break; 是处理文件长度比n小的情况。 // Forward declaration o...

weixin_33912445的博客 427

QSPI Flash的原理与QSPI时序的Verilog实现

本节主要是讨论QSPI(Quad SPI,四线SPI总线)的相关内容。我的开发板上有一片型号是W25Q128BV的Quad SPI Flash存储器,本文将以它为例子来说明QSPI操作的一些内容。

Go Forward Forever! 4188

ubuntu安装glibc

VSCode 在ubuntu下调试无法打开libc-start.c raise.c等1. 安装glibc2. launch.json中添加路径映射3. 调试 1. 安装glibc apt install glibc-source cd /usr/src/glibc/ sudo tar -xvf glibc-[VERSION].tar.xz 2. launch.json中添加路径映射 "sourceFileMap": { "/build/glibc-S7xCS9": "/usr/src/glib

wq_0708的博客 3万+

Leetcode 158 Read N Characters Given Read4 II - Call multiple times

思路: 一开始做的时候做成了157题,但是后来发现这两题不一样,157是只call一次,这题会call多次。更可悲的是用157的思路做,上次自己还做出来了,这次做完了发现还是错的哈哈。无奈没什么思路就去youtube上面看了别人的解释视频,这边是链接:https://www.youtube.com/watch?v=w3ke3MQTEJ8&amp;t=618s。确实很巧妙,我是想不出来暂时。以后复盘的时候有不理解代码的时候可以点击链接上去看看别人解释。下面就直接放代码了: /** * The read..

GoodJobJasper的博客 273

基于强化学习的工业SCR脱硝系统控制算法设计与实现

本文提出了一种基于强化学习的工业SCR脱硝系统智能控制方法。针对SCR系统存在的非线性、大滞后和多变量耦合等控制难点,研究采用混合建模方法构建了精度在5%以内的系统模型,并设计了专门的强化学习环境。该方法通过定义合理的动作空间和状态空间,结合物理约束设计损失函数,利用深度强化学习算法优化氨喷射控制策略。相比传统PID控制,该智能控制器能更好地适应烟气参数波动,实现NOx排放的稳定控制,为工业环保设施提供了更优的自动化解决方案。

huanghm88的专栏 1465

[Leetcode] 158. Read N Characters Given Read4 II - Call multiple times 解题报告

题目: 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 le

魔豆(Magicbean)的博客 1478

四大厂商网络设备巡检命令实战指南:华为、华三、锐捷、思科

本文详细介绍了华为、华三、锐捷、思科四大厂商网络设备的巡检命令实战指南,涵盖硬件状态、软件运行和网络功能等核心维度。通过具体命令示例和实用技巧,帮助网络工程师高效完成设备巡检,预防潜在故障,确保网络稳定运行。

weixin_29235923的博客 398

群晖如何安装软路由OpenWrt/iStoreOS作为旁路由(含网络设置、设置密码、还原出厂设置等linux命令ipk包)

openwrt的固件。

XiaoqiangClub的博客 7047

158. Read N Characters Given Read4 II - Call multiple times

158. Read N Characters Given Read4 II - Call multiple times 题目链接:https://leetcode.com/problems... 和那道read n不同的是这次call multiple times,问题就是当前的call可能存在多读了几个字节,那么下一次call read...

weixin_34168880的博客 148

电子元器件-逻辑器件篇:逻辑电平、CMOS逻辑、手册解读、逻辑电平转换,应用注意事项

常用的逻辑电平TTL、CMOS、LVTTL、LVCOMS、CML、ECL、PECL、LVPECL、LVDS、RS232电平RS422电平,RS485电平等。TTL和CMOS的逻辑电平按典型电压可分为四类:5V系列、3.3V系列,2.5V系列和1.8V系列。5V TTL和5V CMOS逻辑电平是通用的逻辑电平。3.3V及以下的逻辑电平被称为低电压逻辑电平,常用的为LVTTL电平低电压的逻辑电平还有2.5V和1.8V两种。

qq_60226558的博客 869

[LeetCode] 158. Read N Characters Given Read4 II - Call multiple times

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 f...

weixin_30305735的博客 130

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

✡ leetcode 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) ---...

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 f...

weixin_30539835的博客 102

jre9下载 jdk9

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

[LeetCode][Array]121. Best Time to Buy and Sell Stock

Problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Solution1: 对数组进行遍历,一一比较 class Solution { public int maxProfit(int[] prices) { int max = 0; for(int i = 0; i ...

Renoler的博客 414

OV9734 DATASHEET,完整规格书

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

[Leetcode] 269. Alien Dictionary

Problems: https://leetcode.com/problems/alien-dictionary/ Solution: Topological Sort 建立拓扑图并寻找所有字符的入度(buildGraph),再寻找入度为0的字符取出,邻接字符入度减一(bfs) class Solution { public String alienOrder(String[] words...

Renoler的博客 354

[Leetcode] 937. Reorder Data in Log Files

Problems: https://leetcode.com/problems/reorder-data-in-log-files/ 思路: 把log分开考虑,从第二个string开始看是否是digit,分为digit和letter两个list letterList重写sort方法,从第二个string开始按照字母顺序进行排序,需要注意的是如果字母顺序完全相同,则考虑index的字母排序 Solu...

Renoler的博客 341

[Leetcode] 253. Meeting Rooms II

Problems: https://leetcode.com/problems/meeting-rooms-ii/ Solution1: 首先对不同intervals根据开始时间进行排序,再利用priority queue存储不同rooms的结束时间; (* priority queue只能帮助确定peek是最小(或最大)值,queue内其他元素排序未知) 对不同的intervals和peek进行...

Renoler的博客 280

[LeetCode][Array] 53. Maximum Array

Problems: https://leetcode.com/problems/maximum-subarray/ Solution1: Brute Force - Time Limit Exceeded class Solution { public int maxSubArray(int[] nums) { // brute force int ans ...

Renoler的博客 273

[LeetCode][String] 383. Ransom Note

Problem: https://leetcode.com/problems/ransom-note/ Solution: class Solution { public boolean canConstruct(String ransomNote, String magazine) { HashMap&lt;Character, Integer&gt; count = n...

Renoler的博客 216

[Leetcode][BFS/DFS]785. Is Graph Bipartite?

Problems: https://leetcode.com/problems/is-graph-bipartite/ Solution1: DFS 将相邻的点标上不同的颜色,判断依据为相邻两个node不可以颜色相同 利用stack存储邻接node class Solution { public boolean isBipartite(int[][] graph) { in...

Renoler的博客 213

[LeetCode][Array] 42. Trapping Rain Water

Problem: https://leetcode.com/problems/trapping-rain-water/ Solution1: 找出当前列的左边最高列和右边最高列,取较小的列与当前列进行比较,若当前列矮于较小列,则结果累加 class Solution { public int trap(int[] height) { int ans = 0; ...

Renoler的博客 204

[LeetCode] 74. Search a 2D Matrix

Problems: https://leetcode.com/problems/search-a-2d-matrix/ Solution1: class Solution { public boolean searchMatrix(int[][] matrix, int target) { // 矩阵为空,需要注意的是depth和width都需要判断 if(mat...

Renoler的博客 200

[Leetcode] 426. Convert Binary Search Tree to Sorted Doubly Linked List

Problem: https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ Soluiton: 使用inorder traversal读取值,读出来的node和cur double linked 最后再把dummy node删除 class Solution { public...

Renoler的博客 200

[LeetCode][Array] 135. Candy

Problem: https://leetcode.com/problems/candy/ Solution: Brute force 创建数组存储糖果个数,初始值都为1 遍历数组,与前后值分别进行比较 多次遍历,直到所有值都满足条件 class Solution { public int candy(int[] ratings) { int len = ratings...

Renoler的博客 200
上一篇: [Leetcode] 211. Add and Search Word - Data structure design
下一篇: [Leetcode] 138. Copy List with Random Pointer
Shlyan
博客等级 码龄9年 0粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值