[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 ...
随机推荐
- MFC对话框程序EDIT类控件的自动换行,垂直滚动条自动下移
1.新建一个Edit Control,将其Multiline属性设置为True,Auto HScroll属性设置False,这样就可以实现每一行填满后自动换行了. 2.再将Vetrical Scr ...
- selenium IDE处理各种窗口问题解决方法
一.处理模态窗口:showModalDialog 由于弹出模态窗口后,就无法定位到当前窗口的元素和模态窗口的元素,需要添加js解决 模态窗口动作类似下面语句: <input id="c ...
- 在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法
在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法 ==========方法1: --------1. 选相应 ...
- Top命令查看内存
c 切换显示命令名称和完整命令行. M 根据驻留内存大小进行排序 第四行:内存状态 8306544k total — 物理内存总量(8GB) 7775876k used — 使用中的内存总量(7.7G ...
- python 自动化之路 day 06
ATM作业讲解: 数据访问层 业务逻辑层 time & datetime模块 import time # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了 ...
- linux 文件操作编程
Linux中所有的设备和文件的操作都使用文件描述符来进行. 文件描述符是一个非负的整数,它是一个索引值,指向内核中每个进程打开的记录表. 当打开一个文件或者创建一个新文件时,内核就向进程返回一个文件描 ...
- apache .htaccess 伪静态重定向,防盗链 限制下载...
301全站跳转 RewriteEngine OnRewriteCond %{HTTP_HOST} ^www\.old\.net$ [NC]RewriteRule ^(.*)$ http://www.n ...
- 尝试一下用MARKDOWN嵌入代码
public void test(){ // }
- Avoiding “will create implicit index” NOTICE
执行PgSql避免 notice 信息,执行之前加入以下语句调整报错级别即可: SET CLIENT_MIN_MESSAGES = ‘WARNING’;
- 学习PHP爬虫--《Webbots、Spiders和Screen Scrapers:技术解析与应用实践(原书第2版)》
<Webbots.Spiders和Screen Scrapers:技术解析与应用实践(原书第2版)> 译者序 前言 第一部分 基础概念和技术 第1章 本书主要内容3 1.1 发现互联网的真 ...