[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 ...
随机推荐
- Plsql工具单步调试 存储过程或是 函数(oracle数据库)-留着自己用的
<案例1> 原地址: http://jingyan.baidu.com/article/3a2f7c2e144d2826aed61167.html 调试过程对找到一个存过的bug或错误是非 ...
- vs2010安装路径解决不能修改的方法
环境:win7 64位 解决:网上说需要卸载以下4项 Microsoft Visual Studio Tools for Applications 2.0 - ENU Microsoft Visual ...
- C# 泛型2
我们在编写程序时,经常遇到两个模块的功能非常相似,只是一个是处理int数据,另一个是处理string数据,或者其他自定义的数据类型,但我们没有办法,只能分别写多个方法处理每个数据类型,因为方法的参数类 ...
- ejs 基本语法
1.基本语法.<% code %> 无缓冲的条件语句元素.<%= code %> 转义HTML,该code并且会打印出来.<%- code %> ...
- 『重构--改善既有代码的设计』读书笔记----Substitute Algorithm
重构可以把复杂的东西分解成一个个简单的小块.但有时候,你必须壮士断腕删掉整个算法,用简单的算法来取代,如果你发现做一件事情可以有更清晰的方式,那你完全有理由用更清晰的方式来解决问题.如果你开始使用程序 ...
- C# winform 递归选中TreeView子节点
/// <summary> /// 递归选中所有的自节点 /// </summary> /// <param name="nodeThis">T ...
- Fedora 18 安装前指南
Secure Boot 与 Win 8 随着 Win8 的发布,先前关于 Secure Boot 和 UEFI 的诸多猜测也得到了证实,Fedora 18 也将如同当初计划的那样使用 shim + ...
- JavaScript多线程初步学习
一.多线程理解 首先,我们要理解什么是多线程,百度百科上说:多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一 ...
- opencv数据结构总结
OpenCV里面用到了很多图像相关的数据结构,熟练掌握它们是学习图像的基础. 1.IplImage IplImage IplImage IPL 图像头 typedef struct _IplImage ...
- Day14 html简介
初识html <!DOCTYPE html> <html lang="en"> <head> <!--自闭合标签--> <me ...