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的更多相关文章

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

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

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

  5. LeetCode Read N Characters Given Read4

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...

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

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

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

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

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

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

随机推荐

  1. mysql好用的函数

    FIND_IN_SET 用法示意: INSERT INTO `test` VALUES (1, 'name', 'daodao,xiaohu,xiaoqin'); INSERT INTO `test` ...

  2. Error:(1, 1) java: 非法字符: ‘\ufeff’

    一.问题 用IDEA打开eclipse java项目编译时,出现以下错误: Error:(1, 1) java: 非法字符: '\ufeff' Error:(1, 10) java: 需要class, ...

  3. laravel CURD

    检索一个列值列表DB::table("tablename")->lists('mobile'); //5.3 及以上版本 lists 改为 pluck 返回 [ " ...

  4. 程序员,Python 这次彻底上位了!

    在 2018 年底,我们迎来了编程语言的最后一波洗礼.近期 TIOBE 公布了 12 月编程语言排行榜,前三名变为: Java.C.Python. 而在 PYPL 榜单上 Python 在今年 5 月 ...

  5. UWP MySQL 最新版 6.10.5是坏的

    #实锤#证实了,MySQL 最新版 6.10.5,在UWP平台并不能连接,是坏的 Oracle竟然没有测试吗?直接上线??? 我已经把把BUG设置为最高严重等级,提交给了官方. I'm using u ...

  6. Mac电脑如何快速下载YouTube视频

    如果你想下载一些教育类的视频资源,或者是一些学习的教程,那么YouTube是一个很好的视频资源平台.YouTube上面各种各样的资源都有,而且质量都很有保证,尤其是那些订阅量很多的人.可惜的是,You ...

  7. SQL常见面试题

    1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name   kecheng   fenshu张三    语文       81张三     数学       75李四     语文   ...

  8. idea_debug

    条件断点 快捷键 cmd + shift +f8 demo 表达式求值 注意,调试的时候,选中相应变量 alt + f8 demo set value (感觉会非常有用) 调试时直接改变变量的值,快捷 ...

  9. 从Web抓取信息

    来源:python编程快速上手——Al Sweigart webbrowser:是 Python 自带的,打开浏览器获取指定页面. requests:从因特网上下载文件和网页. Beautiful S ...

  10. Bootstrap学习--基本格式

    以下为Bootstrap的基本格式代码 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta ...