[LeetCode#157] Read N Characters Given Read4
Problem:
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 will only be called once for each test case.
Analysis:
This problem is not hard, but it is easy to be wrong.
General Idea:
Since read4(char[] buf) would always filled buf with four characters, no matter how many characters left in the file.
case: read4(char[] buf) may fill buf with "['a', 'b', x00, x00]", if there are only two characters left in the file. Thus we could not directly use "read4" over "char[] buf", and we should take advantage of a temp_buffer for this purpose. Thus we could base on the return int of "read 4" to add the character into buf. There are possible two situation of the end:
1. there are not enough characters left in the file. (for the target n)
2. n was meeted. For this condition, we should maintain a count of copied words.
if (cur_len < 4 || count == n)
break; When we copy the characters from the temp_buffer, we should only copy the valid range.
1. the actual words we have read (may less than 4)
2. iff we have already reach the target n. (may just need part of the characters)
read_len = read4(temp_buffer);
cur_len = Math.min(read_len, n - count); Then we should copy those characters into buf.
for (int i = 0; i < cur_len; i++)
buf[count+i] = temp_buffer[i];
Note: the cur_len's computation is very important along the process!!!
Solution:
public class Solution extends Reader4 {
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
public int read(char[] buf, int n) {
if (buf == null)
throw new IllegalArgumentException("buf is null");
if (n <= 0)
return 0;
int count = 0, read_len = 0, cur_len = 0;
char[] temp_buffer = new char[4];
while (true) {
read_len = read4(temp_buffer);
cur_len = Math.min(read_len, n - count);
for (int i = 0; i < cur_len; i++)
buf[count+i] = temp_buffer[i];
count += cur_len;
if (cur_len < 4 || count == n)
break;
}
return count;
}
}
[LeetCode#157] Read N Characters Given Read4的更多相关文章
- [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 ...
- ✡ leetcode 157. Read N Characters Given Read4 利用read4实现read --------- java
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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
- ✡ 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】157. Read N Characters Given Read4
Difficulty: Easy More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...
- 157. 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 ...
- leetcode[158] Read N Characters Given Read4 II - Call multiple times
想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...
- [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] 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 ...
随机推荐
- 转:四种方案解决ScrollView嵌套ListView问题
转载自:http://blog.sina.com.cn/s/blog_46798aa80101lxbk.html 原始的连接已经不知道是哪里了,项目中遇到了同样的问题,花了一下午都没有想到是嵌套引起的 ...
- mvc给html扩展方法:
mvc给html扩展方法: 注意:扩展方法和所在的类都必须是 public static如果在页面直接使用新扩展的方法,需要web.config里把Web.Helper名称命名空间加上,页面才能访问到 ...
- ORACLE 11g R2数据库安装硬件环境要求
物理内存要求:最小1G,在windows7,windows8,windows8.1上最小2G. 虚拟内存(或分页空间)容量要求: Available RAM Swap Space Required B ...
- linux修改主机名(hostname)转载
Linux修改主机名的方法 用hostname命令可以临时修改机器名,但机器重新启动之后就会恢复原来的值. #hostname //查看机器名#hostname -i //查看本机器名对应的ip ...
- AFNetworking3.0+MBProgressHUD二次封装,一句话搞定网络提示
对AFNetworking3.0+MBProgressHUD的二次封装,使用更方便,适用性非常强: 一句话搞定网络提示: 再也不用担心网络库更新后,工程要修改很多地方了!网络库更新了只需要更新这个封装 ...
- iOS9的那些坑 — — WeiboSDK registerApp启动就崩溃
在升级Xcode7.2.1后,在App加载时直接崩掉,仔细看了,发现是在在注册微博SDK的时候报错: [WeiboSDK registerApp:WBAPPKey]; 查了很多资料,最后在github ...
- html-----015---HTML ASCII 参考手册
HTML 和 XHTML 用标准的 7 比特 ASCII 代码在网络上传输数据. 7 比特 ASCII 代码可提供 128 个不同的字符值. 7 比特 可显示的 ASCII 代码 <html&g ...
- java 环境变量配置
1.我的电脑 2.属性 3.高级 4.环境变量 5.新建 6.JAVA_HOME: C:\Program Files (x86)\Java 7.CLASSPATH: .;%JAVA_HOME%\;%J ...
- bzoj1068:[SCOI2007]压缩
思路:区间dp,设状态f[l][r][bo]表示区间[l,r]的答案,bo=1表示该区间可以放M也可以不放M,bo=0表示该区间不能放M,并且对于任意一个状态f,l和l-1之间均有一个M,于是就可以进 ...
- ASP.NET页面生命周期与控件生命周期
ASP.NET页面生命周期 (1)PreInit 预初始化(2)Init 初始化(3)InitComplete 初始化完成(4)PreLoad 预加载(5)Load 加载(6)LoadComplete ...