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.
/* The read4 API is defined in the parent class Reader4.
int read4(char[] buf); */ public class Solution extends Reader4 {
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
private char[] tmp = new char[4];
private int tmpPoint; //indicate which position we should start reading
private int tmpContain;//indicate how many char in the tmp buff
public int read(char[] buf, int n) {
int cur = 0;
while(cur < n){
if(tmpPoint == 0){
tmpContain = read4(tmp);
}
if(tmpContain == 0){
return cur;
}
while(cur < n && tmpPoint < tmpContain){
buf[cur ++] = tmp[tmpPoint ++];
}
tmpPoint = tmpPoint % tmpContain;
}
return cur;
}
}
Read N Characters Given Read4 II - Call multiple times的更多相关文章
- [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 ...
- 【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 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 用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
想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...
- Leetcode-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 ...
随机推荐
- 洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX
洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX 点击进入FakeHu的模拟退火博客 神仙模拟退火...去看fakehu的博客吧...懒得写了... 因为精度问题要在求得的最优解附近(大约 ...
- idea tomcat热部署 Error running 'Tomcat 7': Unable to open debugger port (127.0.0.1:3622): java.net.SocketExcepti
2018/5/6 经过测试,发现只需要修改 http port 为 8081即可,JMX port 不用改 默认是 1099 今天在进 tomcat 的 debug 模式时报了此异常, tomcat ...
- Windows7共享设置
问题描述:Win7共享文件夹时提示“您没有权限访问xxxx.请与网络管理员联系请求访问权限” 解决方案: 控制面板→网络和共享中心→更改高级共享设置→选中“启用共享以便可以访问网络的用户可以读 ...
- Linux 技巧
Linux Handbook For RedHat Enterprise Linux System System # clean old kernel packages package-cleanup ...
- 【Unity Shader】(十) ------ UV动画原理及简易实现
笔者使用的是 Unity 2018.2.0f2 + VS2017,建议读者使用与 Unity 2018 相近的版本,避免一些因为版本不一致而出现的问题. [Unity Shader](三) ----- ...
- 为什么 jmeter 分布式测试,一定要设置 java.rmi.server.hostname
之前总结了 jmeter 分布式测试的过程,在部署过程中提到,要在 system.properties 中配置自己的 IP. 至于为什么要这么做,源于这一次 debug 的过程. 运行环境 mint, ...
- mtr的用法场景
---引用自阿里云 mtr (My traceroute)也是几乎所有 Linux 发行版本预装的网络测试工具.他把 ping和 traceroute 的功能并入了同一个工具中,所以功能更强大. mt ...
- 阿里云解析记录应对家里动态IP
<?php #需要配置的项 define('ACCESSKEYID',''); #阿里云用户密钥ID 获取方法 https://help.aliyun.com/knowledge_detail/ ...
- fiddler常识汇总
Fiddler 抓包工具总结 名称 含义 # 抓取HTTP Request的顺序,从1开始,以此递增 Result HTTP状态码 Protocol 请求使用的协议,如HTTP/HTTPS/FTP ...
- Scrum Meeting 11.03
成员 今日任务 明日计划 用时 徐越 休息 赵庶宏 编写功能说明书,servlet代码移植 servlet代码移植 3h 薄霖 阅读上一届相关代码,思考改进方法 学习安卓界面设计数据库管理 4 ...