LeetCode Read N Characters Given Read4
原题链接在这里: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的更多相关文章
- [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 用Read4来读取N个字符
The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...
- 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] 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 ...
- 【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] 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 ...
- 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
- [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 ...
- 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 ...
随机推荐
- 使用React重构百度新闻webapp前端
http://wangfupeng.coding.me/share/2016/08/06/restruct-bdnews-webapp-by-react.html
- Codeforces Beta Round #2 A. Winner
A. Winner time limit per test 1 second memory limit per test 64 megabytes input standard input outpu ...
- Codeforces Round #215 (Div. 2) A. Sereja and Coat Rack
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
- 【BZOJ】2693: jzptab
http://www.lydsy.com/JudgeOnline/problem.php?id=2693 题意:求$\sum_{i=1}^{n} \sum_{j=1}^{m} lcm(i, j)$, ...
- 【wikioi】1003 电话连线
题目链接 算法: 最小生成树 PS:被卡过2天(中间的时间没去做).日期:2013-09-13 13:49:47 ~ 2013-09-17 13:01:07 此题为基础题 刚开始学图论时只会用Krus ...
- 【BZOJ1003】1003: [ZJOI2006]物流运输trans SPFA+DP
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- android注意事项
今天做安卓设计,正在学习布局.在过程中遇到了几个小问题,感觉非常有必要记录分享出来. 1.string字符串不要出现"that's" ,要使用“that is”要不然会报错. 2. ...
- weblogic部署项目包,报空指针错误
贴出 报错代码 <weblogic> <> <> <1479765377228> <BEA-240003> <Console enco ...
- AngularJS 乱记
1. 前端简单逻辑 <title data-ng-bind="{true:' ('+notice_count+') '}[notice_count > 0]+{true:glob ...
- selenium grid中的多个线程同步执行
需求:有一个工作流,每一步审批都需要多个领导参与,才能推流程到下一步去 代码思考:多个领导在自己的线程中运行,速度有的快有的慢,如何保证下一步的领导审批时,这个步骤已经激活 如下是代码:思路为:如果这 ...