[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 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个字符的更多相关文章
- ✡ 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 ...
- [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 ...
- 【LeetCode】157. Read N Characters Given Read4
Difficulty: Easy More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...
- 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
- [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 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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- input 时间字段默认值
背景: 时间字段展示默认值,开始时间为当天 0点,结束时间为当天晚上12点 代码: <input style="Width: 180px;float:left ;" type ...
- 大数据之路week07--day07 (Hive结构设计以及Hive语法)
Hive架构流程(十分重要,结合图进行记忆理解)当客户端提交请求,它先提交到Driver,Driver拿到这个请求后,先把表明,字段名拿出来,去数据库进行元数据验证,也就是Metasore,如果有,返 ...
- 利用Python绘制一个正方形螺旋线
1 安装turtle Python2安装命令: pip install turtule Python3安装命令: pip3 install turtle 因为turtle库主要是在Python2中使用 ...
- windows——快速得到某一目录下所有文件的名称
前言 其实用的是windows上dir命令,能快速得到某一目录下的所有文件名称,天天那么忙都没时间写博客(┬_┬) 步骤 打开cmd并cd到某目录下 C:\Users\Administrator.KI ...
- 3.Vue 实例
创建一个 Vue 实例 每个 Vue 应用都是通过用 Vue 函数创建一个新的 Vue 实例开始的: var vm = new Vue({ // 选项 }) 虽然没有完全遵循 MVVM 模型,但是 V ...
- mysql 存储过程的定义和使用
1)创建存储过程:并循环插入数据 create PROCEDURE sp_name() BEGIN DECLARE i int; ; DO ,); ; end while; END
- (13)Go接口
接口(interface)定义了一个对象的行为规范,只定义规范不实现,由具体的对象来实现规范的细节. 接口 接口类型 在Go语言中接口(interface)是一种类型,一种抽象的类型. interfa ...
- 帝国CMS万能标签标题截取后自动加入省略号
帝国CMS万能标签标题截取后自动加入省略号,没有达到字数的则不加省略号完美解决方案1.打开e/class/connect.php 搜索 if(!empty($subtitle))//截取字符 大约 ...
- layui如何自定义弹出层关闭事件
在某些业务场景下,我们需要自定义弹出层关闭事件,代码示例如下: layui.use('layer', function () { var layer = layui.layer; layer.open ...
- 20189220 余超《Linux内核原理与分析》第三周作业
操作系统如何工作的 第二章的基础知识 计算机的三大法宝:存储程序计算机,函数调用堆栈机制,中断. 堆栈:堆栈是C语言程序运行时必须使用的几率函数条用路径和参数存储的空间,具体作用分为:记录函数条用的框 ...