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. [LeetCode] 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. [LeetCode] 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 & 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. Read N Characters Given Read4 II - Call multiple times

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

  7. [LeetCode] 157. 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. [Locked] Read N Characters Given Read4 & 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. Read N Characters Given Read4 解题报告(C++)

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

随机推荐

  1. 使用influx控制台工具操作InfluxDB

    这里记录下influx控制台的简单使用,如需更多功能请参考InfluxDB官方文档: https://docs.influxdata.com/influxdb/v1.1/ 环境: CentOS6.5_ ...

  2. Android内存分析命令(转)

    一.概述 1.1 内存指标概念 Item 全称 含义 等价 USS Unique Set Size 物理内存 进程独占的内存 PSS Proportional Set Size 物理内存 PSS= U ...

  3. java Map常用方法封装

      java Map常用方法封装 CreationTime--2018年7月16日15点59分 Author:Marydon 1.准备工作 import java.util.HashMap; impo ...

  4. linux 上配置tomcat、mysql 开机启动

    1.tomcat 开机启动 方法一.修改系统文件 (已经尝试,可以) 1.修改/etc/rc.d/rc.local vi /etc/rc.d/rc.local 2.添加下面两行脚本,记住是两行,仅仅第 ...

  5. redis对key的基本操作

    不忘初心,励志前行 del key1 key2 ... Keyn作用: 删除1个或多个键返回值: 不存在的key忽略掉,返回真正删除的key的数量 rename key newkey作用: 给key赋 ...

  6. 转:D3DXVec3TransformNormal() 与 3DXVec3TransformCoord() 的区别

    DirectX中有两个很相似的函数,输入与输出的参数格式完全一样,都是输入一个三维向量(D3DXVECTOR3)和一个矩阵(D3DXMATRIX),输出变换之后的向量(D3DXVECTOR3). 函数 ...

  7. ASP.NET MVC3-第02节-添加一个Controller (C#)

    前言 ---------------------------- 第01节[翻译]01-ASP.NET MVC 3介绍 ---------------------------- MVC是“model-v ...

  8. Xcode使用小技巧-filter查找功能和查看最近修改的文件

    今天偶然发现了关于Xcode的一个小技巧: 1.查看最近修改的文件 2.使用filter查找制定文件 没错,就是下面这个东西,很容易忽略的一个小工具,在Xcode左下角位置. 通过这个,我们能够在整个 ...

  9. 【LeetCode】199. Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  10. 【Struts2】Struts2框架的搭建

    1,Struts2简介 struts1和struts2都是由Apache组织发布的,但是比较有趣的是struts2和struts1并没有“血缘关系”.在Apache发布struts1之后,当时是还是非 ...