[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 ...
随机推荐
- Can't connect to MySQL server on 'ip' (13)
解决方法1.:setsebool -P httpd_can_network_connect_db=1 解决方法2.:修改/etc/selinux/config SELINUX=enforcing 为 ...
- 阿里DRUID数据源
Druid是Java语言中最好的数据库连接池.Druid能够提供强大的监控和扩展功能. https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81 ...
- django之创建第4-3个项目-访问list数据
1.index <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- python之模块filecmp(文件/目录比较)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块filecmp(文件/目录比较) #用于比较文件及文件夹的内容.他是轻量级的工具.可以做一 ...
- memcache使用方法测试
<?php //php操作memcache的使用测试总结--学习 //1 Memcache::connect; //$memcache = new Memcache; //$memcache-& ...
- Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境
使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(一) 2015-01-14 20:28 by 飘扬的红领巾, 4322 阅读, 5 评论, 收藏, 编辑 ...
- 实战VMware的三种网络模式
来源于:http://www.aneasystone.com/archives/2015/04/three-network-modes-of-vmware-in-action.html 一.实验目的 ...
- Maven + SpringMVC项目集成Swagger
Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集成到服 ...
- (原)torch模型转pytorch模型
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/7839263.html 目前使用的torch模型转pytorch模型的程序为: https://gith ...
- Spring3.0.3使用之异常解决
2010-10-29 温馨提示: 以下异常仅在Spring3.0.3版本中遇到,其他版本可能也会遇到,读者可作参考.不保证会顺利通过. 近期在学习Spring3的一些 ...