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.

  1. // Forward declaration of the read4 API.
  2. int read4(char *buf);
  3.  
  4. class Solution {
  5. public:
  6. /**
  7. * @param buf Destination buffer
  8. * @param n Maximum number of characters to read
  9. * @return The number of characters read
  10. */
  11. int read(char *buf, int n) {
  12. char tmp[];
  13. int idx = , cnt4;
  14. while (idx < n) {
  15. cnt4 = read4(tmp);
  16. for (int i = ; i < cnt4 && idx < n; ++i) {
  17. buf[idx++] = tmp[i];
  18. }
  19. if (cnt4 < ) break;
  20. }
  21. return idx;
  22. }
  23. };

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.

  1. // Forward declaration of the read4 API.
  2. int read4(char *buf);
  3.  
  4. class Solution {
  5. private:
  6. char tmp[];
  7. int tmp_idx = , tmp_len = ;
  8. public:
  9. /**
  10. * @param buf Destination buffer
  11. * @param n Maximum number of characters to read
  12. * @return The number of characters read
  13. */
  14. int read(char *buf, int n) {
  15. int idx = ;
  16. bool flag;
  17. while (idx < n) {
  18. flag = true;
  19. if (tmp_idx == tmp_len) {
  20. tmp_idx = ;
  21. tmp_len = read4(tmp);
  22. if (tmp_len != ) flag = false;
  23. }
  24. for (; tmp_idx < tmp_len && idx < n; ++tmp_idx) {
  25. buf[idx++] = tmp[tmp_idx];
  26. }
  27. if (!flag) break;
  28. }
  29. return idx;
  30. }
  31. };

[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. 关于Android中50M+的文本入库处理细节

    好久没有写技术文章,明早4点还要爬起来赶飞机,感觉这个坑有必要记录一下,以慰藉一下自己脆弱的灵魂.周一和周二忙了2天的样子才解决这个问题,中间填了不少的坑,反正已经夜深了,慢慢地记录一点. 场景:项目 ...

  2. Plus One 加一运算

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  3. Generate BKS File( Bouncy Castle KeyStore)

    echo "Enter BKS output file name : \c" read filename echo "Enter BKS Password : \c&qu ...

  4. 〖Linux〗让Kubuntu的“启动栏”与Win7“任务栏”的界面和功能一样

    先来展示一下我的桌面效果图: === 是否发现这与Windows 7任务栏非常相似?哈哈- === 背景: 玩久了Unity,想换个品味,就把Ubuntu安装了KDE桌面,发现甚是不错: 这里教大家怎 ...

  5. 一个简单的ExtJS搜索建议框

    封装的是一个Ext4.2的组件,继承并兼容于Ext的ComboBox. 实现原理非常easy,在 combo 中监听 keyup 事件就可以. 搜索建议的Combo.基本上全然兼容, 使用方式与Com ...

  6. django之异常错误3(Student matching query does not exist.)

    错误提示: DoesNotExist at /blog/test2/ Student matching query does not exist. 说明:错误提示说明错误在test2中,查找数据库的表 ...

  7. .NET(C#):警惕PLINQ结果的无序性

    .NET(C#):警惕PLINQ结果的无序性 2012年08月10日 ⁄ 综合 ⁄ 共 620字 ⁄ 字号 小 中 大 ⁄ 评论关闭   PLINQ的运行结果是无序的,也就是不保持原来集合的顺序来操作 ...

  8. JavaScript判断是否全为中文,是否含有中文

    来源于:http://blog.csdn.net/yenange/article/details/7463897 第一种代码(全为中文则返回"true",不全为中文则返回" ...

  9. java计算时间差, 日期差小结

    转自:https://blog.csdn.net/sy793314598/article/details/79544796 1.java 7中的日历类Calendar Calendar类使用其静态的g ...

  10. socket 995 错误 boost

    这个错误的中文解释是:由于线程退出或应用程序请求,已中止 I/O 操作. 最近几天学习boost asio 在抄官方的一个实例代码时遇到 了,这个错误搞了我三天才解决,就是在一行代码中少了一个 s 所 ...