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 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 readsn characters from the file.

Note:
The read function will only be called once for each test case.

分析:

  read函数被调用一次,那么就直接用代码解释这题即可吧。

代码:

int read4(char *buf);
class Solution {
public:
int read(char *buf, int n) {
char *cur = buf;
int clen = , slen = ;
//当还有字符可以读出来时
while((clen = read4(cur))) {
slen += clen;
//当字符数目超出n时,只留下n个
if(slen >= n) {
cur += n + - slen;
break;
}
cur += clen;
}
*cur = '\0';
//当字符数目小于n时,文件就读完了,则返回文件总长;若字符数目大于等于n时,返回n
return slen < n ? slen : n;
}
};

Read N Characters Given Read4 II - Call multiple times

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 may be called multiple times.

分析:

  相比于I,这题要被调用多次,而大量的文件读取操作时间代价很大,为了解决这个问题,可以用一个缓存存储已经读过的字符,我用一个私有的string作为缓存。

代码:

int read4(char *buf);
class Solution {
private:
string str;
public:
Solution() {
str = "";
}
int read(char *buf, int n) {
//利用缓存除去不必要的操作
if(n <= str.length()) {
//将string传给字符串数组
strncpy(buf, str.c_str(), n);
return n;
}
strcpy(buf, str.c_str());
char *cur = buf + str.length();
int clen = , slen = int(str.length());
//当缓存不够用时,继续在文件里读取字符
while((clen = read4(cur))) {
slen += clen;
//当字符数目超出n时,只留下n个
if(slen >= n) {
str.append(cur, n + - slen);
cur += n + - slen;
break;
}
//字符串数组传给string
str.append(cur, clen);
cur += clen;
}
*cur = '\0';
//当字符数目小于n时,文件就读完了,则返回文件总长;若字符数目大于等于n时,返回n
return slen < n ? slen : n;
}
};

注:本题代码没有验证

[Locked] Read N Characters Given Read4 & Read N Characters Given Read4 II - Call multiple times的更多相关文章

  1. 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...

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

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

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

  4. ✡ leetcode 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) --------- java

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

  5. 158. Read N Characters Given Read4 II - Call multiple times

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

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

    想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...

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

  8. [leetcode]158. Read N Characters Given Read4 II - Call multiple times 用Read4读取N个字符2 - 调用多次

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

  9. Read N Characters Given Read4 II - Call multiple times

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

随机推荐

  1. Axiom3D学习日记 2.介绍SceneManager,SceneNode,Entity

    SceneManager(场景管理类) 所有出现在屏幕里的东西都受SceneManager管理(最好是这样),当你放置对象在场景里,SceneManager就会跟踪他们的位置,当你为场景创建一个相机, ...

  2. iOS中MVVM的架构设计与团队协作

    对MVVM的理解主要是借鉴于之前的用过的MVC的Web框架,之前用过ThinkPHP框架,和SSH框架,都是MVC的架构模式,今天MVVM与传统的MVC可谓是极为相似,也可以说是兄弟关系,也就是一家人 ...

  3. 打开自定义链接新窗口(safari JS prompt的坑!)2016.03.08

    很简单的一个小练习,但做的过程中发现safari的一个坑,使用prompt()方法的时候,点击取消和不输入一样,会返回空字符' ',而不是null! 要求: 制作新按钮,"新窗口打开网站&q ...

  4. Ubuntu14.04 Tomcat 安装过程记录

    Ubuntu14.04 Tomcat 安装过程记录 检查java的版本 zhousp@ubuntu:~$ sudo java -version [sudo] password for zhousp: ...

  5. 中级Perl第二章习题

    2. 4. 1. 习题1 [15 分钟] 写一个程序从命令行取一个文件清单, 然后用grep 把那些文件大小在1000 字节以内的文件找出来.用map 把这个清单里的每个字串前加四个空格并在 字串后面 ...

  6. golang bufio writer,reader 缓存规则

    读,写,缓冲区可以杜绝频繁的读,写动作1.写缓存,如果一次write的长度大于buffer长度那么久发送当前缓冲区的内容并且发送要写入的内容,就是不在缓存了.如果发送的内容小于buffer长度,就按缓 ...

  7. 一个中型项目:本地校园App

    好暨: 这个项目的起源于课堂老师作业的要求.老师要求一年下来完成一个构想并实现Demo.思考良久,在要提交构想的那个晚上,想到了校园App,当时团队只有两个人,但我感觉到:就是它了!项目启动时间——2 ...

  8. JS作用域概念-预解析规则

    // 作用域: // 域:空间.范围.区域…… // 作用:读.写 script 全局变量.全局函数 自上而下 函数 由里到外 {} 浏览器: “JS解析器” 1)“找一些东西” :var funct ...

  9. 关于alarm函数

    #include<unistd.h> #include<signal.h> void handler() { printf("Hello\n"); sign ...

  10. centos postfix 邮箱安装记录

    ---恢复内容开始--- #wget http://nchc.dl.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-2.9 ...