[LeetCode] Read N Characters Given Read4 用Read4来读取N个字符
Given a file and assume that you can only read the file using a given method read4
, implement a method to read n characters.
Method read4:
The API read4
reads 4 consecutive characters from the file, then writes those characters into the buffer array buf
.
The return value is the number of actual characters read.
Note that read4()
has its own file pointer, much like FILE *fp
in C.
Definition of read4:
Parameter: char[] buf
Returns: int Note: buf[] is destination not source, the results from read4 will be copied to buf[]
Below is a high level example of how read4
works:
File file("abcdefghijk"); // File is "abcdefghijk", initially file pointer (fp) points to 'a'
char[] buf = new char[4]; // Create buffer with enough space to store characters
read4(buf); // read4 returns 4. Now buf = "abcd", fp points to 'e'
read4(buf); // read4 returns 4. Now buf = "efgh", fp points to 'i'
read4(buf); // read4 returns 3. Now buf = "ijk", fp points to end of file
Method read:
By using the read4
method, implement the method read
that reads n characters from the file and store it in the buffer array buf
. Consider that you cannot manipulate the file directly.
The return value is the number of actual characters read.
Definition of read:
Parameters: char[] buf, int n
Returns: int Note: buf[] is destination not source, you will need to write the results to buf[]
Example 1:
Input: file = "abc", n = 4
Output: 3
Explanation: After calling your read method, buf should contain "abc". We read a total of 3 characters from the file, so return 3. Note that "abc" is the file's content, not buf. buf is the destination buffer that you will have to write the results to.
Example 2:
Input: file = "abcde", n = 5
Output: 5
Explanation: After calling your read method, buf should contain "abcde". We read a total of 5 characters from the file, so return 5.
Example 3:
Input: file = "abcdABCD1234", n = 12
Output: 12
Explanation: After calling your read method, buf should contain "abcdABCD1234". We read a total of 12 characters from the file, so return 12.
Example 4:
Input: file = "leetcode", n = 5
Output: 5
Explanation: After calling your read method, buf should contain "leetc". We read a total of 5 characters from the file, so return 5.
Note:
- Consider that you cannot manipulate the file directly, the file is only accesible for
read4
but not forread
. - The
read
function will only be called once for each test case. - You may assume the destination buffer array,
buf
, is guaranteed to have enough space for storing n characters.
这道题给了我们一个 Read4 函数,每次可以从一个文件中最多读出4个字符,如果文件中的字符不足4个字符时,返回准确的当前剩余的字符数。现在让实现一个最多能读取n个字符的函数。这题有迭代和递归的两种解法,先来看迭代的方法,思路是每4个读一次,然后把读出的结果判断一下,如果为0的话,说明此时的 buf 已经被读完,跳出循环,直接返回 res 和n之中的较小值。否则一直读入,直到读完n个字符,循环结束,最后再返回 res 和n之中的较小值,参见代码如下:
解法一:
// Forward declaration of the read4 API.
int read4(char *buf); class Solution {
public:
int read(char *buf, int n) {
int res = ;
for (int i = ; i <= n / ; ++i) {
int cur = read4(buf + res);
if (cur == ) break;
res += cur;
}
return min(res, n);
}
};
下面来看递归的解法,这个也不难,对 buf 调用 read4 函数,然后判断返回值t,如果返回值t大于等于n,说明此时n不大于4,直接返回n即可,如果此返回值t小于4,直接返回t即可,如果都不是,则直接返回调用递归函数加上4,其中递归函数的 buf 应往后推4个字符,此时n变成n-4即可,参见代码如下:
解法二:
// Forward declaration of the read4 API.
int read4(char *buf); class Solution {
public:
int read(char *buf, int n) {
int t = read4(buf);
if (t >= n) return n;
if (t < ) return t;
return + read(&buf[], n - );
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/157
类似题目:
Read N Characters Given Read4 II - Call multiple times
参考资料:
https://leetcode.com/problems/read-n-characters-given-read4/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Read N Characters Given Read4 用Read4来读取N个字符的更多相关文章
- [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 ...
- LeetCode Read N Characters Given Read4
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...
- [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 ...
- [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 ...
- LeetCode初级算法--字符串02:字符串中的第一个唯一字符
LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...
- LeetCode Read N Characters Given Read4 II - Call multiple times
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...
- ✡ 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] Read N Characters Given Read4 I & II
Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...
随机推荐
- LinqToDB 源码分析——DataContext类
LinqToDB框架是一个轻量级的ORM框架.当然,功能上来讲一定比不上Entity Framework的强大.但是在使用上总让笔者感觉有一点Entity Framework的影子.笔者想过可能的原因 ...
- Hive安装部署
目录 一. 安装Hive. 1 1. 选择CDH版本的... 1 2. 解压文件... 1 二. 配置Hive. 1 1. 配置环境变量 ...
- C# 系统托盘图标
C# 系统托盘图标 WPF NotifyIcon 资料 网址: http://www.codeproject.com/Articles/36468/WPF-NotifyIcon http://www. ...
- MahApps.Metro使用
# MahApps.Metro使用 # ## 下载MahApps.Metro ## PM> Install-Package MahApps.Metro ## MainWindow.xaml中添加 ...
- canvas调用scale或者drawImage图片操作后,锯齿感很明显的解决
<script type="text/javascript"> //解决canvas画画图片 var mengvalue = -1; var phoneWidth = ...
- 多线程并发同一个表问题(li)
现有数据库开发过程中对事务的控制.事务锁.行锁.表锁的发现缺乏必要的方法和手段,通过以下手段可以丰富我们处理开发过程中处理锁问题的方法.For Update和For Update of使用户能够锁定指 ...
- Servlet的生命周期及工作原理
Servlet生命周期分为三个阶段: 1,初始化阶段 调用init()方法 2,响应客户请求阶段 调用service()方法 3,终止阶段 调用destroy()方法 Servlet初始化阶段: 在 ...
- Atitit 自动化gui 与 发帖机 技术
Atitit 自动化gui 与 发帖机 技术 1.1. Gui tech1 1.2. 自动化软件测试1 1.3. selenium attilax1 1.4. 图形脚本语言Sikuli1 1.5. ...
- JS高程4.变量,作用域和内存问题(3)垃圾收集
JavaScript的自动垃圾收集机制 执行环境会负责管理代码执行过程中使用的内存,编写JavaScript程序时,所需内存的分配以及无用内存的回收完全实现自动管理. 原理: 找出那些不再继续使用的变 ...
- ipython notebook 浏览器中编写数学公式和现实
Python Notebook简介1 http://www.cnblogs.com/cbscan/p/3545084.html $ python -m IPython http://pypi.pyth ...