原题链接在这里: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. Java集合框架学习总结

    转自:http://www.cnblogs.com/oubo/archive/2012/01/07/2394639.html Oubo的博客 以下介绍经常使用的集合类,这里不介绍集合类的使用方法,只介 ...

  2. 20145308刘昊阳 《Java程序设计》实验四 Android环境搭建 实验报告

    20145308刘昊阳 <Java程序设计>实验四 Android环境搭建 实验报告 实验名称 Android环境搭建 实验内容 搭建Android环境 运行Android 修改代码,能输 ...

  3. [知识点]网络流之Dinic算法

    // 此博文为迁移而来,写于2015年2月6日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vrg4.html      ...

  4. 【BZOJ】2563: 阿狸和桃子的游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=2563 题意:给一个n个加权点m条加权边的无向图,两个人轮流拿走一个点,最后使先手得分-后手得分尽量大 ...

  5. 在Javascript中监听flash事件(转)

    在Javascript中监听flash事件,其实有两种做法: 1.在特定的环境下(例如专门制作的flash),大家约定一个全局函数,然后在flash的事件中用ExternalInterface.cal ...

  6. Bootstrap整合ASP.NET MVC验证、jquery.validate.unobtrusive

    没什么好讲的,上代码: (function ($) { var defaultOptions = { validClass: 'has-success', errorClass: 'has-error ...

  7. Winform 窗体最小化隐藏在桌面右下角:转

    ICO文件要放到 bin\Debug 下 1.给主窗体添加 NotifyIcon 控件 2.窗体加载事件里 private void MainF_Load(object sender, EventAr ...

  8. MongoDB数据库的简介及安装

    一.MongoDB数据库简介 简介 MongoDB是一个高性能,开源,无模式的,基于分布式文件存储的文档型数据库,由C++语言编写,其名称来源取自“humongous”,是一种开源的文档数据库──No ...

  9. About_MySQL Select--来自copy_03

    MySQL Select   查询分类 单表查询:简单查询 多表查询:连接查询 联合查询:多个查询结果汇总 查询的组成 投影查询:挑选要显示的字段 select array1,array2,... f ...

  10. script标签不带属性与带async、defer的区别

    <script> 当页面解析到script标签时,会停止解析并下载对应的脚本,并马上执行,执行完毕后再继续解析页面 <script async> async 在下载脚本的同时不 ...