Read N Characters Given Read4 II - Call multiple times
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 may be called multiple times.
/* 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
*/
private char[] tmp = new char[4];
private int tmpPoint; //indicate which position we should start reading
private int tmpContain;//indicate how many char in the tmp buff
public int read(char[] buf, int n) {
int cur = 0;
while(cur < n){
if(tmpPoint == 0){
tmpContain = read4(tmp);
}
if(tmpContain == 0){
return cur;
}
while(cur < n && tmpPoint < tmpContain){
buf[cur ++] = tmp[tmpPoint ++];
}
tmpPoint = tmpPoint % tmpContain;
}
return cur;
}
}
Read N Characters Given Read4 II - Call multiple times的更多相关文章
- [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] 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 II - Call multiple times
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...
- 【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 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 ...
- 158. Read N Characters Given Read4 II - Call multiple times
题目: 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 用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[158] Read N Characters Given Read4 II - Call multiple times
想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...
- Leetcode-Read N Characters Given Read4 II
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
随机推荐
- Apache和Nginx比较
Apache和Nginx对比 功能对比 Nginx和Apache一样,都是HTTP服务器软件,在功能实现上都采用模块化结构设计,都支持通用的语言接口,如PHP.Perl.Python等,同时还支持正向 ...
- MySQL数据库常用操作语法
1. 数据库初始化配置 1.1. 创建数据库 create database apps character set utf8 collate utf8_bin;创建数据库”app“,指定编码为utf8 ...
- PageHelper分页插件使用
mybatis的分页插件jar包: 配置方法: 在mybatis配置文件中加下面代码 <plugin interceptor="com.github.pagehelper.PageIn ...
- 用人工智能学习,凡亿推出PCB问题解答智能搜索机器人:pcb助手
对于学习者,你是不是经常遇到这样的问题:在我们狠狠下定决心学习PCB技术的时候,我们常常遇到很多大大小小的问题,遗憾的是身边没有一个能及时给自己解答问题的高手指点,通过论坛.群等方式询问可能半天也得不 ...
- NO--14 微信小程序,左右联动二
上一篇讲解了左=>右联动,那个还比较简单,本篇写剩下比较核心的部分,也是本次开发过程中遇到最难的部分,右=>左联动,先简单看一下演示 右左联动.gif 一.关键技术: (1) 小程序 ...
- 木马分析出现python语言,360的安全人员不禁感叹还有这种操作?
几年前,敲诈者木马还是一个默默无闻的木马种类.然而,由于其极强的破坏力和直接且丰厚的财富回报,敲诈者木马这几年已经一跃成为曝光率最高的木马类型——甚至超越了盗号木马.远控木马.网购木马这传统三强.与此 ...
- 【Unity】 Cursor学习
CursorLockMode.None 光标行为未修改,第一人称视角下鼠标可以突破窗口. CursorLockMode.Locked 光标锁定到游戏窗口的中心,与全屏与否无关,同时隐藏光标(这一点在3 ...
- 从一个简单的寻路问题深入Q-learning
这第一篇随笔实际上在我的科学网博客上是首发,我重新拿到博客园再发一次是希望以此作为我学习Q-learning的一个新的开始.以后这边主技术,科学网博客主理论.我也会将科学网那边技术类的文章转过来的.希 ...
- Appengine直接下载文件并保存到google drive
一直对下载文件比较感兴趣.前些日子无意搜到google 推出一项服务,可以直接将文件下载到google drive中,原型猛戳这里,但有限额限制.一时脑洞大开,可不可以在appengine 上架设服务 ...
- exec命令详解
基础命令学习目录首页 原文链接: exec: 在bash下输入man exec,找到exec命令解释处,可以看到有”No new process is created.”这样的解释,这就是说exec命 ...