[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 ...
随机推荐
- 自定义控件学习 Day44
自定义控件学习 Day44 onMeasure 测量控件的宽高. onLayout 设置位置 onDarw 绘制控件 问题堆栈 1. 事件监听传递 最外层获取到控件,根据事件事件传递机制,返回值fal ...
- A题笔记(12)
No.1466 代码:https://code.csdn.net/snippets/192091 No.1202 代码:https://code.csdn.net/snippets/192110 ...
- Power Map 更新日志
2015-05-18,五月更新 Custom Regions feature,允许用户自定义区域要素,支持kml和shape格式 New customization features,包括图例/文本框 ...
- 一、VSTO概述
一.什么是VSTO? VSTO = Visual Studo Tools for Office,是.net平台下的Office开发技术.相对于传统的VBA(Visual Basic Applicati ...
- C++ Union妙用(将列表初始化用于数组元素)
Union是个不被注意的关键字,意为联合体,这是个诡异的名字.若不是为了继承C语言,它也不会出现在C++中(虽说,union在C++中得到了扩充,完成了接近类的功能).它的作用主要是节省内存空间,在嵌 ...
- mysql error笔记1
mysql视图问题: The user specified as a definer ('root'@'%') does not exist 原因:由于root用户对全局host无访问权限,给root ...
- javascript闭包的理解
闭包是Javascript的一个难点,但也是一个很重要的知识点. 1.首先我们要知道变量作用域链 变量的作用域分两种:全局变量和局部变量.没有定义到任何函数中的变量为全局变量,在函数中定义的变量为局部 ...
- Codevs 1507 酒厂选址
1507 酒厂选址 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description Abstinence(戒酒)岛的居民们酷爱一种无酒精啤酒 ...
- js获取json数据
var json = { contry:{ area:{ man:"12万", women:"10万" } } };//方式一:使用eval解析 var ...
- HTML css面试题
1.对WEB标准以及W3C的理解与认识 标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索几率.使用外链css和js脚本.结构行为表现的分离.文件下载与页面速度更快.内容能被更多的用户所访问.内容能被更 ...