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.

给一个Read4函数,每次可以从一个文件中最多读出4个字符,如果文件中的字符不足4个,返回当前剩余的字符。实现一个读取n个字符的函数。

用一个临时数组,存放每次read4读到字符,再用一个指针标记buf数组目前存储到的位置,然后将这个临时数组的内容存到buf相应的位置就行了。

需要注意两个corner case:

如果本次读到多个字符,但是我们只需要其中一部分就能完成读取任务时,我们要拷贝的长度是本次读到的个数和剩余所需个数中较小的

如果read4没有读满4个,说明数据已经读完,这时候对于读到的数据长度,因为也可能存在我们只需要其中一部分的情况,所以要返回总所需长度和目前已经读到的长度的较小的

Java: Time: O(n), Space: O(1)

public class Solution extends Reader4 {
public int read(char[] buf, int n) {
for(int i = 0; i < n; i += 4){
char[] tmp = new char[4];
// 将数据读入临时数组
int len = read4(tmp);
// 将临时数组拷贝至buf数组,这里拷贝的长度是本次读到的个数和剩余所需个数中较小的
System.arraycopy(tmp, 0, buf, i, Math.min(len, n - i));
// 如果读不满4个,说明已经读完了,返回总所需长度和目前已经读到的长度的较小的
if(len < 4) return Math.min(i + len, n);
}
// 如果循环内没有返回,说明读取的字符是4的倍数
return n;
}
}

Python:

# The read4 API is already defined for you.
# @param buf, a list of characters
# @return an integer
def read4(buf):
global file_content
i = 0
while i < len(file_content) and i < 4:
buf[i] = file_content[i]
i += 1 if len(file_content) > 4:
file_content = file_content[4:]
else:
file_content = ""
return i class Solution(object):
def read(self, buf, n):
"""
:type buf: Destination buffer (List[str])
:type n: Maximum number of characters to read (int)
:rtype: The number of characters read (int)
"""
read_bytes = 0
buffer = [''] * 4
for i in xrange(n / 4 + 1):
size = read4(buffer)
if size:
size = min(size, n-read_bytes)
buf[read_bytes:read_bytes+size] = buffer[:size]
read_bytes += size
else:
break
return read_bytes if __name__ == "__main__":
global file_content
buf = ['' for _ in xrange(100)]
file_content = "a"
print(buf[:Solution().read(buf, 9)])
file_content = "abcdefghijklmnop"
print(buf[:Solution().read(buf, 9)])

C++:

// Time:  O(n)
// Space: O(1) 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) {
int read_bytes = 0;
char buffer[4];
for (int i = 0; i <= n / 4; ++i) {
if (int size = read4(buffer)) {
size = min(size, n - read_bytes);
for (int j = 0; j < size; ++j) {
buf[read_bytes++] = buffer[j];
}
} else {
break;
}
}
return read_bytes;
}
};

  

  

  

相关题目:

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

最新文章

  1. 将 ASP.NET MVC3 Razor 项目部署到虚拟主机中
  2. [转载]jquery中attr和prop的区别
  3. Android高级第十一讲之不同系统间的区别
  4. centos7忘记root密码修改方式
  5. sizeof()用法汇总【转载】
  6. PHP与MySQL中编码的设置
  7. 教程-Delphi第三方控件安装卸载指南
  8. XJOI网上同步训练DAY6 T1
  9. Unity该插件NGUI学习(1)—— 环境结构
  10. http://www.cnblogs.com/gaojing/archive/2011/08/23/2413616.html
  11. 通用的contain函数
  12. vue常用的网址
  13. Atlas安装配置
  14. How tomcat works 读书笔记十二 StandardContext 上
  15. Linux时间子系统之(十五):clocksource
  16. 利用div绘制细线居中
  17. Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine
  18. synchronized(八)
  19. 【转】BFC是什么
  20. 使用 NodeJS+Express+MySQL 实现简单的增删改查

热门文章

  1. 电脑重装后 python 2 3快速安装
  2. WebService(一)
  3. 转发: JS中的call()和apply()方法和区别 --小白变色记
  4. java接口的成员变量的修饰符
  5. 11-Flutter移动电商实战-首页_屏幕适配方案和制作
  6. js判断是否是在微信浏览器中打开
  7. learning scala repreated parameters
  8. circus 架构
  9. python 使用xpath解析含有命名空间(xmlns)的xml
  10. Flutter 简介(事件、路由、异步请求)