原题链接在这里:https://leetcode.com/problems/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.

题解:

Given API read4, 一次最多可以read 4个char, 并把这些char保留在temp Buff中.

Ask to design another API, 能一次最多读n个char. 每次call read4, 若是返回小于4, 说明已经到了end of file, 下一次跳出while loop.

readCount计数当前共读了多少char, n-readCount就是还需要读多少个char.

Time Complexity: O(n). Space: O(1).

AC Java:

 /* 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) {
boolean eof = false;
char [] temp = new char[4];
int readCount = 0;
while(!eof && readCount<n){
int count = read4(temp); //read4 API读的byte都存在temp Buff中
eof = count < 4; count = Math.min(count, n-readCount); //如果n减掉已经读的char的数目比count小,就只读n-readCount个char
for(int i = 0; i<count; i++){
buf[readCount++] = temp[i];
}
}
return readCount;
}
}

跟上Read N Characters Given Read4 II - Call multiple times.

LeetCode Read N Characters Given Read4的更多相关文章

  1. [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 ...

  2. [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 ...

  3. LeetCode Read N Characters Given Read4 II - Call multiple times

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...

  4. [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 ...

  5. 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...

  6. [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 ...

  7. 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 编程思想┊从实例谈面向对象编程(OOP)、工厂模式和重构

    有了翅膀才能飞,欠缺灵活的代码就象冻坏了翅膀的鸟儿.不能飞翔,就少了几许灵动的气韵.我们需要给代码带去温暖的阳光,让僵冷的翅膀重新飞起来.结合实例,通过应用OOP.设计模式和重构,你会看到代码是怎样一 ...

  2. BZOJ3468 : 滑雪

    根据公式$x^k=\sum_{i=1}^k Stirling2(k,i)i!C(x,i)$, 设$f[i][j][k]$表示从$(i,j)$出发的所有路径的$C(路径长度,k)$的和, 根据$C(n, ...

  3. 使用Jaxb2进行xml与bean的转义时Date的format设置

    参考http://jackyrong.iteye.com/blog/1826699 JAXB转换JAVA OBJECT到XML的时候,对java.util.Date的转换有些要注意的地方 输出的格式为 ...

  4. 利用百度云盘API上传文件至百度云盘

    一.获取Access Token示例 1. 请您将以下HTTP请求直接粘贴到浏览器地址栏内,并按下回车键. https://openapi.baidu.com/oauth/2.0/authorize? ...

  5. ACM: 限时训练题解-Runtime Error-二分查找

    Runtime Error   Bahosain was trying to solve this simple problem, but he got a Runtime Error on one ...

  6. [Cocos2D-x For WP8]Sprite精灵

    精灵(Sprite)是游戏里面的角色,比如敌人,游戏里面运动的物体等等,所以精灵是游戏里面一个非常常见的概念,几乎无处不在.在Cocos2D-x里面精灵是用CCSprite类来进行表示的,它可以用一张 ...

  7. 使用SQLAlchemy对Firebird数据库进行操作

    来这个公司已经一周了,度过了开始的无聊日子准备正式准备做点东西了,这几天接触了一下文件数据库InterBase,尝试在Ubuntu上连接其开源版本Firebird,因为公司使用的是SQLAlchemy ...

  8. 51nod算法马拉松12

    A 第K大区间 不妨考虑二分答案x,则问题转化成计算有多少个区间满足众数出现的次数>=x. 那么这个问题我们使用滑动窗口,枚举右端点,则左端点肯定单调递增,然后维护一个简单的数组就能资瓷添加元素 ...

  9. 使用plsql创建用户并授权(图形化界面)

    使用sys用户登录数据库(或者有dba权限的[还不知道具体的区别,但是能用]) 在左边的对象列表中找到USERS,右键点击USERS,选择“新建用户”选项 其他安装下面的图片步骤来即可: OK!

  10. Java基础:继承,封装,多态,抽象类,接口

    只要是从事Java语言有关的开发工作,在面试中难念会被问到这几个东西. 博主学习java有两年多了,算是浅显的知道一些,抄写了一些解释分享一下. 1.什么是面向对象?(面对女朋友) 面向对象(Obje ...