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

[LeetCode] 157. Read N Characters Given Read4 用Read4来读取N个字符的更多相关文章

  1. ✡ leetcode 157. Read N Characters Given Read4 利用read4实现read --------- java

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

  2. [LeetCode#157] Read N Characters Given Read4

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

  3. 【LeetCode】157. Read N Characters Given Read4

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...

  4. 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...

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

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

  7. 157. 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 ...

  8. [Swift]LeetCode157.用Read4来读取N个字符 $ 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 actua ...

  9. [Swift]LeetCode158. 用Read4来读取N个字符II $ 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. jquery 表单对象属性筛选选择器

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  2. dt系统中tag如何使用like与%来进行模糊查询

    在destoon中,如果一个品牌的详细显示页,如果要显示与品牌相关的供应的话,可以通过查询标题中带有品牌关键字的这一条件来进行查询,但是经过测试发现不能正确解析, 然后查看文件的源文件,发现 {tag ...

  3. 用python做数据分析pandas库介绍之DataFrame基本操作

    怎样删除list中空字符? 最简单的方法:new_list = [ x for x in li if x != '' ] 这一部分主要学习pandas中基于前面两种数据结构的基本操作. 设有DataF ...

  4. node爬虫爬取中文时乱码问题 | nodejs gb2312、GBK中文乱码解决方法

    iconv需要依赖native库,这样一来,在一些不支持native模块安装的虚拟主机和windows平台上,我们还是无法安心处理GBK编码. 老外写了一个通过纯Javascript转换编码的模块 i ...

  5. select2实现多选 并且回显

    html代码:<select name="ruleId" id="ruleId" class="required" onchange= ...

  6. HBase学习笔记之BulkLoad

    HBase学习之BulkLoad bulkload的学习以后再写文章. 参考资料: 1.https://blog.csdn.net/shixiaoguo90/article/details/78038 ...

  7. HDU - 5513 Efficient Tree(轮廓线DP)

    前言 最近学了基于连通性的状压DP,也就是插头DP,写了几道题,发现这DP实质上就是状压+分类讨论,轮廓线什么的也特别的神奇.下面这题把我WA到死- HDU-5531 Efficient Tree 给 ...

  8. AJax的三种响应

    AJax的响应 1.普通文本方式(字符串) resp.getWriter().print("你好"); 2.JSON格式当要给前台页面传输 集合或者对象时 使用普通文本传输的时St ...

  9. js之大文件分段上传、断点续传

    文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...

  10. C# VS 调试 动态加载的 DLL

    原文:https://www.cnblogs.com/DasonKwok/p/10510218.html 在这篇文章的底部,有提供示例的Demo,可以参考一下哦,拿来直接就可以运行. 说明: 编译类库 ...