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. Linux 自动更新时间

    1. 从NTP上把时间同步到本地 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 更新本地时间 ntpdate us.pool.ntp.o ...

  2. 工欲善其事必先利其器之Xcode高效插件和舒适配色

    功能强大的Xcode再配上高效的插件,必会让你的开发事半功倍.直接进入正题. Xcode插件安装方式: 1.github下载插件然后用xcode打开运行一遍,然后重启xcode. 2.安装插件管理Al ...

  3. Java操作hbase总结

    用过以后,总得写个总结,不然,就忘喽. 一.寻找操作的jar包. java操作hbase,首先要考虑到使用hbase的jar包. 因为咱装的是CDH5,比较方便,使用SecureCRT工具,远程连接到 ...

  4. 292. Nim Game(C++)

    292. Nim Game(C++) You are playing the following Nim Game with your friend: There is a heap of stone ...

  5. SGU 113.Nearly prime numbers

    水一个代码: #include <iostream> using namespace std; int n, a; bool ok; bool prime (int x) { ; i * ...

  6. SGU 114.Telecasting station

    题意: 百慕大的每一座城市都坐落在一维直线上.这个国家的政府决定建造一个新的广播电视台.经过了许多次试验后,百慕大的科学家们提出了一个结论,在每座城市的不满意度等于这座城市的市民数与这座城市与广播电视 ...

  7. 【BZOJ1146】【树链剖分+平衡树】网络管理Network

    Description M 公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通 信网络.该网络的结构由N个 ...

  8. windows7 64 位 mysql 5.6.12 安装

    1.修改 my-default.ini 改名为 my.ini 内容修改为 [mysqld] loose-default-character-set = utf8    basedir = D:/mys ...

  9. splice从数组中删除指定定数据

    /*从数组中删除指定定数据var somearray = ["mon", "tue", "wed", "thur"]so ...

  10. Bootstrap_表单_按钮

    一.多标签支持 一般制作按钮除了使用<button>标签元素之外,还可以使用<input type="submit">和<a>标签等. 同样,在 ...