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. /* The read4 API is defined in the parent class Reader4.
  2. int read4(char[] buf); */
  3.  
  4. public class Solution extends Reader4 {
  5. /**
  6. * @param buf Destination buffer
  7. * @param n Maximum number of characters to read
  8. * @return The number of characters read
  9. */
  10. private char[] tmp = new char[4];
  11. private int tmpPoint; //indicate which position we should start reading
  12. private int tmpContain;//indicate how many char in the tmp buff
  13. public int read(char[] buf, int n) {
  14. int cur = 0;
  15. while(cur < n){
  16. if(tmpPoint == 0){
  17. tmpContain = read4(tmp);
  18. }
  19. if(tmpContain == 0){
  20. return cur;
  21. }
  22. while(cur < n && tmpPoint < tmpContain){
  23. buf[cur ++] = tmp[tmpPoint ++];
  24. }
  25. tmpPoint = tmpPoint % tmpContain;
  26. }
  27. return cur;
  28. }
  29. }

Read N Characters Given Read4 II - Call multiple times的更多相关文章

  1. [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 ...

  2. [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 ...

  3. LeetCode Read N Characters Given Read4 II - Call multiple times

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...

  4. 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times

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

  5. ✡ leetcode 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) --------- java

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  6. 158. 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 ...

  7. [leetcode]158. Read N Characters Given Read4 II - Call multiple times 用Read4读取N个字符2 - 调用多次

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  8. leetcode[158] Read N Characters Given Read4 II - Call multiple times

    想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...

  9. Leetcode-Read N Characters Given Read4 II

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

随机推荐

  1. 4552: [Tjoi2016&Heoi2016]排序

    4552: [Tjoi2016&Heoi2016]排序 链接 分析: 因为只询问一次,所以考虑二分这个数.显然是没有单调性的,但是我们可以二分所有大于等于mid的数中,是否有满足条件的x(而不 ...

  2. centos7 安装postgres9.4

    1.安装postgres资源:> yum install https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_ ...

  3. 大数据中HBase的Java接口封装

    该文前提为已经搭建好的HBase集群环境,参见 HBase集群搭建与配置 ,本文主要是用Java编写一个Servlet接口,部署在Tomcat服务器上,用于提供http的接口供其他地方调用,接口中集成 ...

  4. mysql查询当天,前一天,一周,一个月

    当天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ...

  5. UNITY_委托和事件

    UNITY_委托和事件 参考资料: Unity3D脚本编程-使用C#语言开发跨平台游戏-陈嘉栋 观察者模式 主题(Subject)管理某些数据,当主题的数据发生改变时,会通知已经注册(Register ...

  6. RabbitMQ入门:路由(Routing)

    在上一篇博客<RabbitMQ入门:发布/订阅(Publish/Subscribe)>中,我们认识了fanout类型的exchange,它是一种通过广播方式发送消息的路由器,所有和exch ...

  7. 布线问题 (NYOJ38)

    布线问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件:1.把所有 ...

  8. Docker--删除容器实例和镜像

    一.删除容器实例 使用命令docker rm 容器ID或者容器名 1.docker ps -a查询已有的实例 [root@cxt data]# docker ps -a 2.docker rm 容器I ...

  9. Navicat新建查询,系统找不到指定路径 独家解决办法

    Navicat新建查询系统找不到指定路径,很多人用了网上流行的那些解决办法,还是无法解决.比如: https://jingyan.baidu.com/article/86112f1387a713273 ...

  10. iOS中使用RNCryptor对资源文件加密(先加密后拖进项目中)

    概述:IPA 在发布时,业务相关的敏感资源文件以明文的形式存储,由于没有加密保护,这些文件在应用发布后 可能被其他人获取,并结合其他漏洞和手段产生真实攻击.所以我们要 1.在设计.开发阶段,集合业务确 ...