[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 ...
随机推荐
- Premature optimization is the root of all evil.
For all of we programmers,we should always remember that "Premature optimization is the root of ...
- TSQL Beginners Challenge 3 - Find the Factorial
这是一个关于CTE的应用,这里我们用CTE实现阶乘 Factorial,首先来看一个简单的小实验,然后再来看题目.有的童鞋会问怎么没有2就来3了呢,惭愧,TSQL Beginners Challeng ...
- c# 为什么要用 get set 属性
1 可以对赋值 做验证 ,范伟限制,额外的限制 2 可以设置 只读 只写 3 可以做线程同步 4 可以将属性设置在interface接口中 5 可以使用虚属性 或 抽象属性 可以填补 没有 虚字段 抽 ...
- Linux 自动更新时间
1. 从NTP上把时间同步到本地 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 更新本地时间 ntpdate us.pool.ntp.o ...
- 工欲善其事必先利其器之Xcode高效插件和舒适配色
功能强大的Xcode再配上高效的插件,必会让你的开发事半功倍.直接进入正题. Xcode插件安装方式: 1.github下载插件然后用xcode打开运行一遍,然后重启xcode. 2.安装插件管理Al ...
- UITextView/UITextField检测并过滤Emoji表情符号
UITextView/UITextField检测并过滤Emoji表情符号 本人在开发过程中遇到过这种情况,服务器端不支持Emoji表情,因此要求客户端在上传用户输入时,不能包含Emoji表情.在客户端 ...
- Debug your C# project more efficiently
I am a very beginner working with C# and Visual Studio 2013. When I debug my project, I always reope ...
- hadoop 分片与分块,map task和reduce task的理解
分块:Block HDFS存储系统中,引入了文件系统的分块概念(block),块是存储的最小单位,HDFS定义其大小为64MB.与单磁盘文件系统相似,存储在 HDFS上的文件均存储为多个块,不同的是, ...
- 主成份分析PCA
Data Mining 主成分分析PCA 降维的必要性 1.多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯. 2.高维空间本身具有稀疏性.一维正态分布有6 ...
- arm Linux 系统调用过程
系统调用是操作系统提供给用户(应用程序)的一组接口,每个系统调用都有一个对应的系统调用函数来完成相应的工作.用户通过这个接口向操作系统申请服务,如访问硬件,管理进程等等.但是因为用户程序运行在用户空间 ...