[LeetCode] Read N Characters Given Read4 I & II

时间:2021-08-19 08:39:09

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.

 // 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) {
char tmp[];
int idx = , cnt4;
while (idx < n) {
cnt4 = read4(tmp);
for (int i = ; i < cnt4 && idx < n; ++i) {
buf[idx++] = tmp[i];
}
if (cnt4 < ) break;
}
return idx;
}
};

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

 // Forward declaration of the read4 API.
int read4(char *buf); class Solution {
private:
char tmp[];
int tmp_idx = , tmp_len = ;
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 idx = ;
bool flag;
while (idx < n) {
flag = true;
if (tmp_idx == tmp_len) {
tmp_idx = ;
tmp_len = read4(tmp);
if (tmp_len != ) flag = false;
}
for (; tmp_idx < tmp_len && idx < n; ++tmp_idx) {
buf[idx++] = tmp[tmp_idx];
}
if (!flag) break;
}
return idx;
}
};

[LeetCode] Read N Characters Given Read4 I & II的更多相关文章

  1. &lbrack;LeetCode&rsqb; 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 actu ...

  2. LeetCode Read N Characters Given Read4 II - Call multiple times

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...

  3. &lbrack;LeetCode&rsqb; Read N Characters Given Read4 用Read4来读取N个字符

    The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...

  4. LeetCode Read N Characters Given Read4

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...

  5. Read N Characters Given Read4 I &amp&semi; II

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  6. 【LeetCode】158&period; Read N Characters Given Read4 II - Call multiple times

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...

  7. &lbrack;LeetCode&rsqb; 157&period; Read N Characters Given Read4 用Read4来读取N个字符

    The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...

  8. &lbrack;Locked&rsqb; Read N Characters Given Read4 &amp&semi; Read N Characters Given Read4 II - Call multiple times

    Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...

  9. 【LeetCode】157&period; Read N Characters Given Read4 解题报告&lpar;C&plus;&plus;&rpar;

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...

随机推荐

  1. 解决IDEA中Maven加载依赖包慢的问题

    原理:maven加载jar包过程,默认的是先扫描本地仓库,若本地仓库没有,则扫描远程仓库下载.默认的conf/settings.xml文件没有配置远程仓库,所以扫描的是maven的*仓库(在国外), ...

  2. Linux 定时任务

    200 ? "200px" : this.width)!important;} --> 介绍 本篇主要介绍Linux定时任务命令crontab的用法,crontab是定时任务 ...

  3. LintCode 整数排序

    题目:给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法. 1.冒泡:从头开始,比较相邻的两个元素,大的放在后面.一轮结束之后,最大的数沉底,不参与下一轮比较. ...

  4. MongoDB固定集合(Capped Collections)

    MongoDB 固定集合(Capped Collections)是性能出色且有着固定大小的集合,对于大小固定,我们可以想象其就像一个环形队列,当集合空间用完后,再插入的元素就会覆盖最初始的头部的元素! ...

  5. 『土地征用 Land Acquisition 斜率优化DP』

    斜率优化DP的综合运用,对斜率优化的新理解. 详细介绍见『玩具装箱TOY 斜率优化DP』 土地征用 Land Acquisition(USACO08MAR) Description Farmer Jo ...

  6. django 小东小西

    1.request.META里包含了哪些数据? request.META 是一个Python字典,包含了所有本次HTTP请求的Header信息,比如用户IP地址和用户Agent(通常是浏览器的名称和版 ...

  7. vim替换tab到4空格

    ~/.vimrc 中设置 # 设置tab宽度 set ts= #也可以用 set tabstop= #空格替换tab set expandtab 如果对于已保存的文件 :%retab! !用于修改所有 ...

  8. 学习C语言,在软件测试中如何用?

    1)为什么学? 掌握基础: 编写测试脚本: 自动化: 性能测试: 看懂代码,定位问题(白盒测试). C语言如何开发有界面的程序? 首先给大家扫盲:1 什么叫做界面程序:   归根到底就是设置LCD上的 ...

  9. ArcGIS10&period;6了解一下

    因为计算机水平不断更新,ESRI不得不重新倾力打造下一代ArcMap,叫ArcGIS Pro,现在ArcGIS Pro功能有一定地突显,但还不够强大和稳定:而ArcGIS Desktop方面没有什么大 ...

  10. 15、BigDecimal类简介

    BigDecimal类概述 由于在运算的时候,float类型和double很容易丢失精度,在金融.银行等对数值精度要求非常高的领域里面,就不能使用float或double了,为了能精确的表示.计算浮点 ...