[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 ...
随机推荐
- java集合之链式操作
如果用过js/jquery.groovy等语言,大概对这样的代码比较熟悉: [1,2,3].map(function(d){...}).grep(function(d){...}).join(',') ...
- NSDate,NSCalendar,NSTimer,NSTimeZone
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. ...
- 百度地图API地址转换成经纬度
public class LngAndLatUtil { public static Map<String,Double> getLngAndLat(String address){ Ma ...
- 从零开始学java(猜数字游戏)
练练手不喜勿喷,看到什么学习什么第一次发博客格式就见见谅..... 2016-07-21 19:55:02 imp ...
- Linux下JDK环境变量配置
JDK官方下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 我的下载路 ...
- nim
上帝创造了一个n*m棋盘,每一个格子都只有可能是黑色或者白色的. 亚当和夏娃在玩一个游戏,每次寻找边长为x的正方形,其中每个格子必须为黑色,然后将这些格子染白. 如果谁不能操作了,那么那个人就输了. ...
- IE下空链接失效原因及解决方法
我们把a链接的display设置为block,但如果对该标签设置为position:absolute后,会发现在ie6.ie7下有时点击无效,ie8下有效(ie8标准),使用zoom:1方式也无法解决 ...
- NameNode元数据的管理机制(三)
元数据的管理: 第一步:客户端通过DistributedFilesystem 对象中的creat()方法来创建文件,此时,RPC会 通过一个RPC链接协议来调用namenode,并在命名空间中创建一个 ...
- bzoj2734: [HNOI2012]集合选数
Description <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中 ...
- python学习的一些感悟
首先声明,这里用到的库是在coursera上学习时,老师建立的一个库 #timer import simplegui import random # global state message = &q ...