[Locked] Read N Characters Given Read4 & Read N Characters Given Read4 II - Call multiple times
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的更多相关文章
- 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- [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 II - Call multiple times
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...
- ✡ 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 ...
- 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 ...
- leetcode[158] Read N Characters Given Read4 II - Call multiple times
想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...
- [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]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 ...
- 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 ...
随机推荐
- Echarts使用随笔(1)-Echarts中markPoint的使用(静态、动态)-effect
先看一段关于初始化Echart js的使用 myChart = echarts.init(document.getElementById('mainChart')); var o ...
- 部分A+B_1
正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...
- JSONModel的基本使用
JSONModel 是一个库,它能智能并且快速的创建出数据 model,你可以在你的 iOS 项目或者 OSX 项目上使用它. 使用前准备 添加 JSONModel 到你的工程中 1.需要的环境: A ...
- jQuery 停止动画
jQuery stop() 方法用于在动画或效果完成前对它们进行停止. 停止滑动 点击这里,向上/向下滑动面板 实例 jQuery stop() 滑动演示 jQuery stop() 方法. jQue ...
- 中级Perl第二章习题
2. 4. 1. 习题1 [15 分钟] 写一个程序从命令行取一个文件清单, 然后用grep 把那些文件大小在1000 字节以内的文件找出来.用map 把这个清单里的每个字串前加四个空格并在 字串后面 ...
- Thread.sleep(0)的意义
我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: 假设现在是 2008-4-7 12:00:00.000,如果我调用 ...
- An endpoint configuration section for contract "serviceReferenc.service" could not be loaded
场景:有一个WCF应用,添加服务引用后,自动生成一个app.config文件,当调用WCF时,它抛出一个错误: An endpoint configuration section for contra ...
- windows2008 R2 安装wampserver
1. 在官网http://www.wampserver.com/下载,wampserver2.5; 2. 安装时候会缺少msvcr110.dll文件,所以先要安装这个文件: 3. 先从微软下载Visu ...
- 一个关于导出excel模板的实例
1 首先jsp页面 点击模板下载,会自动下载模板excel,效果如下 让我们看源码: 1 jsp页面 <div class="tab-pane" id="profi ...
- loadView与viewDidLoad不同 && loadView学习总结
loadView学习总结 UIViewController类或其子类会在初始化时创建一个UIView对象,会作为控制器的默认视图显示出来,可以通过self.view寻址访问.但没有调用loadView ...