原题链接在这里: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. 旋转轮子 UIActivityIndicatorView

    旋转轮子 *activityView = [[UIActivityIndicatorView alloc ]initWithActivityIndicatorStyle:UIActivityIndic ...

  2. [WP8.1UI控件编程]Windows Phone XAML页面的编译

    1.1.2 XAML页面的编译 Windows Phone的应用程序项目会通过Visual Studio完成XAML页面的编译,在程序运行时会通过直接链接操作加载和解析XAML,将XAML和过程式代码 ...

  3. 【JAVA】Quartz 任务调度和异步执行器

    Quartz基础结构         Quartz对任务调度的领域问题进行了高度抽象,提出了调度器(Scheduler).任务(Job)和触发器(Trigger)这3个核心概念,并在Trigger触发 ...

  4. BZOJ4518: [Sdoi2016]征途

    Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜 ...

  5. iOS 开发技巧总结

    1.添加定时器的常用代码 - (void)delayEnableTabButton { self.tabChannelButton.enabled = NO; [self appendTimer]; ...

  6. gui学习

    GUI 图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面 有名的实现 : ucgui   . 实现围绕几个问题: ...

  7. 简单打包 ipa 方式!

    应用的发布也分两种 一种是.打包成ipa上传到国内第3方软件市场,当用户的手机已经JailBreak时,双击下载的ipa文件就可以安装软件 (ipa同android的apk包一样,实质是一个压缩包) ...

  8. springmvc项目中java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener 严重: Error co ...

  9. GNU for x86汇编语法

    作者:冯老师,华清远见嵌入式学院讲师. 译自“Using as The GNU Assembler January 1994”. 参考Tornado随机文档“GNU Toolkit User's Gu ...

  10. xargs

    xargs的作用一般等同于大多数Unix shell中的反引号,但更加灵活易用,并可以正确处理输入中有空格等特殊字符的情况.对于经常产生大量输出的命令如find.locate和grep来说非常有用.