[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 ...
随机推荐
- 如何搭建javaweb 开发环境
一.准备工作: (1) 环境要求: 1.java jdk 2.eclipse--j2ee版 3.tomcat 4.mysql 5.HeidiSQL_7.0 (2)搭建步骤: 1.安装JDK,配置环境变 ...
- Android测试分析二
什么是android测试,分为黑盒测试和白盒测试. 黑盒就是测试人员看不到代码的,针对需求而进行的一系列测试动作,看代码所展现出来的效果是否和需求一样,或者有什么意外的情况没有处理等,一般开发交给测试 ...
- JavaMail组件实现邮件功能
实现邮件收发功能需要3个jar包: 1.JavaMail组件保内的mail.jar和smtp.jar包 2.JAF组件包里的activition.jar. 复制到WebRoot/WEB-INF/lib ...
- weblogic9.2重置密码
1.删除DefaultAuthenticatorInit.ldift 2.执行命令:java -cp /home/weblogic/bea/weblogic92/server/lib/weblogic ...
- OS X EL Capitan安装Cocoapods 报错ERROR
升级OS X EL Capitan10.11之后,原来的pod不能用了,重新安装cocoapods,发现 在运行 “sudo gem install cocoapods” 的时候出现问题: ERROR ...
- OC - 3.OC的三大特性
一.封装 1> 封装的定义 隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别 2> 封装的好处 可以通过set方法防止为成员变量设置不合理的值 仅向外部提供公 ...
- 第3章 Struts2框架--1、Struts2环境搭建
第3章 Struts2框架--1.Struts2环境搭建 搭建步骤: 1.从下载http://struts.apache.org 没找到Struts2.3.16版,就下载了2.3.29 2.拷贝后解压 ...
- bzoj4330:JSOI2012 爱之项链
题目大意:一串项链由n个戒指组成,对于每个戒指,一共有M个点,R种颜色,且旋转后相同的戒指是相同的,然后一串项链又由N个戒指组成,同时要满足相邻的两个戒指不能相同,这串项链上某个位置插入了一个特殊的东 ...
- ASP.NET页面生命周期与控件生命周期
ASP.NET页面生命周期 (1)PreInit 预初始化(2)Init 初始化(3)InitComplete 初始化完成(4)PreLoad 预加载(5)Load 加载(6)LoadComplete ...
- foreach的一点理解
首先什么样的数据才能实现foreach 1 实现IEnumerable这个接口 2 有GetEnumerable()这个方法 然后为啥实现这个接口或者有这个方法就可以实现foreach遍历 首先我先用 ...