[LeetCode] 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 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的更多相关文章
- [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 ...
- LeetCode Read N Characters Given Read4 II - Call multiple times
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...
- [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 ...
- LeetCode Read N Characters Given Read4
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...
- 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 ...
- 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- [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 ...
- [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 ...
- 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
随机推荐
- 如何使用 MSBuild.exe 生成解决方案中的特定目标
以前都是直接使用VS或者msbuild生成整个解决方案,或者只构建单个工程. 这回使用msbuild构建单个工程的时候出现了问题,因为工程中使用了SolutionDir这个宏来定位第三方库路径. 对于 ...
- php Socket表单提交学习一下
<?php //发送请求指定的页面 $file = "1.php"; $filename = "gitignore.txt"; //文件名 $path = ...
- 初始化ArrayList的两种方法[转]
方式一: ArrayList<String> list = new ArrayList<String>(); String str01 = String("str ...
- Mysql中字符串正确的连接方法
虽然SQL server和My sql的语句基本都一致,但是仍然存在一些小区别.就如字符串的连接来说,SQL server中的字符串连接是使用“+”来连接,不带引号sql server是做加法运算.而 ...
- Linux 系统 fstab错误导致系统无法启动的修复
fstab错误的修复 vim /etc/fstab/dev/sda6 /mnt xfs defaults 0 0重启后系统无法启动,等待一段时间后输入root的密码可进入单用户模式,修改fstab后可 ...
- NSDictionary的分类
@implementation NSDictionary (extra) //根据key值的到字典中的object - (id)getObjectByKey:(NSString*)key { NSAr ...
- [转载]eclipse自动同步插件filesync的使用
原文地址:eclipse自动同步插件filesync的使用作者:老孙丢了金箍棒 这篇文章和之前我写的<eclipse下自动部署WEB项目>根本目的是一样的,只是达到目的的方式不同. ...
- tensorflow代码中的一个bug
tensorflow-gpu版本号 pip show tensorflow-gpu Name: tensorflow-gpu Version: 1.11.0 Summary: TensorFlow i ...
- 树莓派进阶之路 (024) - windows远程桌面连接树莓派通过xrdp服务(转)
本文转载:http://www.cnblogs.com/edgexie/p/6527992.html 在网上看到很多关于windows远程桌面连接树莓派的教程.我也按照教程试过了,遇到了几个坑.特意记 ...
- c语言之函数参数传递之数组篇(转)
在VC中写程序都习惯了,一般数组作为函数实参时,最常用的方法就是引用和指针的方法,但是到C语言中就没有引用了,还有一种比较常用的方法: #include <stdio.h>void sor ...