[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 ...
随机推荐
- 关于Android中50M+的文本入库处理细节
好久没有写技术文章,明早4点还要爬起来赶飞机,感觉这个坑有必要记录一下,以慰藉一下自己脆弱的灵魂.周一和周二忙了2天的样子才解决这个问题,中间填了不少的坑,反正已经夜深了,慢慢地记录一点. 场景:项目 ...
- Plus One 加一运算
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- Generate BKS File( Bouncy Castle KeyStore)
echo "Enter BKS output file name : \c" read filename echo "Enter BKS Password : \c&qu ...
- 〖Linux〗让Kubuntu的“启动栏”与Win7“任务栏”的界面和功能一样
先来展示一下我的桌面效果图: === 是否发现这与Windows 7任务栏非常相似?哈哈- === 背景: 玩久了Unity,想换个品味,就把Ubuntu安装了KDE桌面,发现甚是不错: 这里教大家怎 ...
- 一个简单的ExtJS搜索建议框
封装的是一个Ext4.2的组件,继承并兼容于Ext的ComboBox. 实现原理非常easy,在 combo 中监听 keyup 事件就可以. 搜索建议的Combo.基本上全然兼容, 使用方式与Com ...
- django之异常错误3(Student matching query does not exist.)
错误提示: DoesNotExist at /blog/test2/ Student matching query does not exist. 说明:错误提示说明错误在test2中,查找数据库的表 ...
- .NET(C#):警惕PLINQ结果的无序性
.NET(C#):警惕PLINQ结果的无序性 2012年08月10日 ⁄ 综合 ⁄ 共 620字 ⁄ 字号 小 中 大 ⁄ 评论关闭 PLINQ的运行结果是无序的,也就是不保持原来集合的顺序来操作 ...
- JavaScript判断是否全为中文,是否含有中文
来源于:http://blog.csdn.net/yenange/article/details/7463897 第一种代码(全为中文则返回"true",不全为中文则返回" ...
- java计算时间差, 日期差小结
转自:https://blog.csdn.net/sy793314598/article/details/79544796 1.java 7中的日历类Calendar Calendar类使用其静态的g ...
- socket 995 错误 boost
这个错误的中文解释是:由于线程退出或应用程序请求,已中止 I/O 操作. 最近几天学习boost asio 在抄官方的一个实例代码时遇到 了,这个错误搞了我三天才解决,就是在一行代码中少了一个 s 所 ...