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:

  1. Consider that you cannot manipulate the file directly, the file is only accesible for read4 but not for read.
  2. The read function will only be called once for each test case.
  3. 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/

https://leetcode.com/problems/read-n-characters-given-read4/discuss/49557/Accepted-clean-java-solution

https://leetcode.com/problems/read-n-characters-given-read4/discuss/49496/Another-accepted-Java-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

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

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

  2. LeetCode Read N Characters Given Read4

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...

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

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

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

  6. LeetCode初级算法--字符串02:字符串中的第一个唯一字符

    LeetCode初级算法--字符串02:字符串中的第一个唯一字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog. ...

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

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

  8. ✡ 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 ...

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

随机推荐

  1. CSS 基础篇、绝对有你想要

    本章内容: 简介 CSS 定义 四种引入方式 样式应用的顺序 选择器(Selector) * 通用元素选择器 标签选择器 class 类选择器 # ID选择器 , 多元素选择器 后代元素选择器 > ...

  2. 从.NET和Java之争谈IT这个行业

    一.有些事情难以回头 开篇我得表名自己的立场:.NET JAVA同时使用者,但更加偏爱.NET.原因很简单 1.NET语言更具开放性,从开源协议和规范可以看出; 2.语言更具优势严谨; 3.开发工具V ...

  3. sql 修改字段默认值

    1.查出该字段的约束名称 SELECT c.name FROM sysconstraints a INNER JOIN syscolumns b on a.colid=b.colid INNER JO ...

  4. C#开发微信门户及应用(24)-微信小店货架信息管理

    在前面微信小店系列篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及<C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试& ...

  5. C++ map的基本操作和使用

    原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...

  6. java类的初始化顺序

    在java中,当我们new一个对象时,对象中的成员,初始化块以及构造方法的加载是有一定的顺序的,看下面一副图: 一.单类(无基类)下的初始化顺序: public class Parent { stat ...

  7. android 自定义控件——(二)圆形按钮,圆形View

    ----------------------------------↓↓圆形按钮,圆形View(源代码下有属性解释)↓↓---------------------------------------- ...

  8. Android无需申请权限拨打电话

    Android打电话有两种实现方法: 第一种方法,拨打电话跳转到拨号界面.源代码如下: Intent intent = new Intent(Intent.ACTION_DIAL); Uri data ...

  9. Android Studio快速开发之道

    概述 现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的,今天就根据自己的经验来给大家介绍一下Android Studio快速开发之道. P ...

  10. JQuery plugin ---- simplePagination.js API

    CSS Themes "light-theme" "dark-theme" "compact-theme" How To Use Step ...