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 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.
这题用C 写应该简单很多。
/* The read4 API is defined in the parent class Reader4.
int read4(char[] buf); */ 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) {
int result = 0;
char[] tmp = new char[4];
while(result < n){
int size = super.read4(tmp);
for(int i = 0; i < size && result < n; i ++, result ++){
buf[result] = tmp[i];
}
if(size < 4) break;
}
return result;
}
}
Read N Characters Given Read4的更多相关文章
- [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用
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 ...
- Read N Characters Given Read4 I & II
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 II - Call multiple times
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...
- LeetCode Read N Characters Given Read4
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...
- [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 ...
- [Locked] Read N Characters Given Read4 & Read N Characters Given Read4 II - Call multiple times
Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...
- 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- [LeetCode] Read N Characters Given Read4 I & II
Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...
- [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 ...
随机推荐
- [IOI2018] werewolf 狼人 kruskal重构树,主席树
[IOI2018] werewolf 狼人 LG传送门 kruskal重构树好题. 日常安利博客文章 这题需要搞两棵重构树出来,这两棵重构树和我们平时见过的重构树有点不同(据说叫做点权重构树?),根据 ...
- NLPIR分词(c++接口整理)
C/C++接口——对应的各个函数 函数1-bool NLPIR_Init(const char * sInitDirPath=0, int encoding=GBK_CODE,constchar*sL ...
- idea中 和outline相似的功能
ctrl+ F12 alt+7 ctrl+h 这个用来看继承关系很好
- 处于同一域中的两台SQL Server 实例无法连接
处于同一个域中的两台Sql server 实例无法连接,报的错误信息如下: A network-related or instance-specific error occurred while es ...
- eclipse下载与安装并测试
下载地址:www.ecplise.org 下载完成后双击安装 安装完成之后,第一次运行eclipse会弹出Workspace Launcher对话框,要求设置工作空间存放项目文档. ...
- 利用VBS脚本实现Telnet自动连接
把以下代码保存为*.vbs文件,替换IP.用户名.密码. Dim objShell Set objShell = CreateObject("Wscript.Shell") obj ...
- CSS动画transform、transition和animation的区别
CSS3属性中关于制作动画的三个属性:Transform,Transition,Animation. 1.transform:描述了元素的静态样式,本身不会呈现动画效果,可以对元素进行 旋转rotat ...
- PHP 用户密码加密函数password_hash
传统的用户名和密码都采用加盐的方式存储加密信息,盐值也需要存储. 自PHP5.5.0之后,新增加了密码散列算法函数(password_hash),password_hash() 使用足够强度的单向散列 ...
- JMeter各个基础组件简介
刚从LoadRunner转到JMeter,对JMeter的各种概念比较懵.在这里记录下.欢迎大家关注我的个人微信号:测试杂货铺. JMeter的各个功能都是它的组件来完成或实现的,下面来对JMeter ...
- Git使用教程(一)---本地git管理
之前写过不少小demo,因为没有很好的整理,很多demo都找不到了,必要的时候又要重新写这些demo:这是一个很头疼的问题,很烦躁.网上看到git的版本控制以及托管,很nice.开始学习使用git,关 ...