[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 ...
随机推荐
- C#微信公众号开发 -- (三)用户关注之后自动回复
通过了上一篇文章之后的微信开发者验证之后,我们就可以做微信公众号的代码开发了. 当我们点击关注某个公众号的时候,有时候会发现他会自动给我们回复一条消息,比如欢迎关注XXX公众号.这个功能其实是在点击关 ...
- 二维码QRCode
package com.aig.ecompass.ecard; import java.awt.image.BufferedImage; import java.io.File; import jav ...
- Mysql下在某一列后即表的某一位置添加新列的sql语句
Mysql简介 MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司.MySQL被广泛地应用在Internet上的中小型网站中.由于其体积小.速度快.总体拥有成本低,尤 ...
- 误删除了Oracle的dbf文件后的解决方法
问题描述: 误删除Oracle数据库的dbf文件,在启动和关闭数据库是会提示错误. startup启动数据库时提示: ORA-01157:无法标识/锁定数据文件 ORA-01110:数据文件:‘... ...
- 互联网金融爬虫怎么写-第一课 p2p网贷爬虫(XPath入门)
版权声明:本文为博主原创文章,未经博主允许不得转载. 相关教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 手把手教你写电商爬虫-第三课 ...
- Qt 操作Excel
Qt对Excel的数据读/写操作没有现存的类,需要使用QAxObject,下面是从网上下载下来的一个封装好的类,感觉还可以,一般情况下够用,拿来给大家分享. 头文件: #ifndef EXCELENG ...
- 快速使用Log4Cpp
封了一下接口,快速使用. 其他的你都不用管了. 这里封装了需要读取外部conf文件配置输出项.否则可以用getInstance初始化日志类 #include "L4Cpp.h" v ...
- 【原】jQuery编写插件
分享一下编写设置和获取颜色的插件,首先我将插件的名字命名为jquery.color.js.该插件用来实现以下两个功能1.设置元素的颜色.2.获取元素的颜色. 先在搭建好如下编写插件的框架: ;(fun ...
- UIPageControll - 图片格式
设置pageCon的显示风格: 1. 颜色 page.pageIndicatorTintColor = [UIColor redColor]; page.currentPageIndicatorTin ...
- PS制作独特火焰立体文字
效果图中的文字部分并不复杂,为简单的立体字,用图层样式及手工复制就可以做好.火焰部分稍微有点复杂,用动感及火焰素材叠加,然后再加上火花及炫光等渲染出动感效果即可.最终效果 素材下载:本教程中需要用到的 ...