leetcode[158] Read N Characters Given Read4 II - Call multiple times
想了好一会才看懂题目意思,应该是:
这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作。上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n,并且将相应的字符存在buf里。现在调用多次的话就可能存在以下的例子:
例如文件case是:1,2,3,4,5,6,7
如果要实现read5,先用read4读四个到buf,再用read4读剩下的3个到buf+4之后,但是read5一次最多读5个到buf,所以read4多读的2个就要存起来,防止下次调用read5的时候用。
参见这里,用全局变量记录之前访问的是否有溢出。
// Forward declaration of the read4 API.
int read4(char *buf); class Solution {
public:
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
Solution() : buf_len() { }
int read(char *buf, int n) {
char buffer[];
int cnt = ;
if (buf_len > ) {
memcpy(buf, _buf, min(buf_len, n));
cnt += min(buf_len, n);
if (n < buf_len) {
memcpy(_buf, _buf + n, buf_len - n);
buf_len -= n;
} else {
buf_len = ;
}
}
int sz;
while(cnt < n) {
sz = read4(buffer);
memcpy(buf + cnt, buffer, sz);
cnt += sz;
if (sz < ) break;
}
if (cnt > n) {
buf[n] = '\0';
buf_len = cnt - n;
memcpy(_buf, buffer + (sz-buf_len), buf_len);
cnt = n;
}
return cnt;
}
private:
int buf_len;
char _buf[];
};
leetcode[158] Read N Characters Given Read4 II - Call multiple times的更多相关文章
- ✡ 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 ...
- [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 ...
- 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- [LeetCode] 157. Read N Characters Given Read4 用Read4来读取N个字符
The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...
随机推荐
- BZOJ 1823 JSOI 2010 盛宴 2-SAT
标题效果:有着n材料的种类,m陪审团. 每种材料具有两种不同的方法.每个法官都有两个标准.做出来的每一个法官的菜必须至少满足一个需求. 问:是否有这样一个程序. 思考:2-SAT经典的内置图形问题.因 ...
- 杭州电 1203 I NEED A OFFER!
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 爬虫(heritrix框架)
Heritrix 下载 目前 Heritrix 的最新版本是 1.14.4(2010-5-10 发布),您可以从 SourceForge(http://sourceforge.net/projects ...
- System.arraycopy--findbugs检查引发的更改
EI2: This code stores a reference to an externally mutable object into the internal representation o ...
- POJ 1066 Treasure Hunt(相交线段&&更改)
Treasure Hunt 大意:在一个矩形区域内.有n条线段,线段的端点是在矩形边上的,有一个特殊点,问从这个点到矩形边的最少经过的线段条数最少的书目,穿越仅仅能在中点穿越. 思路:须要巧妙的转换一 ...
- 【超酷超实用】CSS3可滑动跳转的分页插件制作教程
原文:[超酷超实用]CSS3可滑动跳转的分页插件制作教程 今天我要向大家分享一款很特别的CSS3分页插件,这款分页插件不仅可以点击分页按钮来实现分页,而且可以滑动滑杆来实现任意页面的跳转,看看都非常酷 ...
- 《得知opencv》注意事项——矩阵和图像处理——cvOr,cvOrS,cvrReduce,cvRepeat,cvScale,cvSet and cvSetZero
矩阵和图像的操作 (1)cvOr函数 其结构 void cvOr(//两个矩阵相应元素做或执行 const CvArr* src1,//矩阵1 const CvArr* src2,//矩阵2 CvAr ...
- Cloudera CDH 5集群搭建(yum 方式)
1 集群环境 主节点 master001 ~~ master006 从节点 slave001 ~~ slave064 2 安装CDH5的YUM源 rpm -Uvhhttp://ar ...
- Android编码标准
只是练习是很难找工作那会儿在,简历基本上都是赶出去石沉大海,因为有项目开发没有实践经验.没有牛逼的背景.更重要的是,没有真才实学,虽然我在大学时读的是计算机专业,它可以被认为是一个技术教育.但--你知 ...
- Ubuntu 12.04 64bit 安装编译GCC 4.1.2 绝对原创
1. 下载并解压源代码: wget http://mirrors.ustc.edu.cn/gnu/gcc/gcc-4.1.2/gcc-4.1.2.tar.bz2 tar jxvf gcc-4.1.2. ...