【LeetCode】157. Read N Characters Given Read4
Difficulty: Easy
More:【目录】LeetCode Java实现
Description
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.
Intuition
题意:int read4(char[] buffer):该函数功能是读取某个文件,每次读取最多4个字符到buffer中,同时返回读取字符个数。要求利用read4()函数来实现read(char[] buf, int n)函数,总共读取n个字符到buf中。
要求很容易实现,每次用read4()来读取字符,用System.arraycopy(src, srcPos, dest, destPos, length)来复制数组即可。关键要注意的是文件的字符数小于n或者大于n的情况。
Solution
public int read(char[] buf,int n) {
char[] buffer = new char[4];
int index=0;
boolean endOfFile=false;
while(index<n && !endOfFile) {
int size=read4(buffer);
if(size<4)
endOfFile=true;
int bytes=Math.min(size, n-index);
System.arraycopy(buffer, 0, buf, index, bytes);
index+=bytes;
}
return index;
}
Complexity
Time complexity : O(n)
Space complexity : O(1)
What I've learned
1.
More:【目录】LeetCode Java实现
【LeetCode】157. Read N Characters Given Read4的更多相关文章
- 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
- 【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】1002. Find Common Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【leetcode】1032. Stream of Characters
题目如下: Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data ...
- 【leetcode】1002. Find Common Characters
题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【leetcode】955. Delete Columns to Make Sorted II
题目如下: We are given an array A of N lowercase letter strings, all of the same length. Now, we may cho ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
随机推荐
- CTSC&APIO2018游记
Day-1 布吉岛干什么,好像只看了Splay Day0 再次布吉岛干什么,好像也只看了Splay 然后上了火车 wc没买方便面,只能吃40元的盒饭 半夜睡不着,那应该是我太菜了 Day1 九点下火车 ...
- 第16月第24天 find iconv sublime utf-8
1. find . -type f -exec echo {} \; find src -type f -exec sh -c "iconv -f GB18030 -t UTF8 {} &g ...
- IO流总结笔记一
IO流继承关系图 IO概述 IO流是用来处理设备上数据的输入输出. 具体设备有:硬盘,内存,键盘录入等等. IO流的具体分类: 1,根据处理的数据类型不同分为:字节流和字符流,字节流读取的最小单位 ...
- 复习python
1当命令行键入python a.py的方式运行python程序时候,a.py不需要执行权,当已./a.py运行时,需要执行权 2.与c语言不同的地方 i = 3 print (i)#合法 #在pyth ...
- Node、PHP、Java 和 Go 服务端 I/O 性能PK
http://blog.csdn.net/listen2you/article/details/72935679
- Android APP常见的5类内存泄露及解决方法
1.static变量引起的内存泄漏 因为static变量的生命周期是在类加载时开始 类卸载时结束,也就是说static变量是在程序进程死亡时才释放,如果在static变量中 引用了Activity 那 ...
- Informatic学习总结_day02
1.sort组件(排序) 2.aggregater组件 聚集组件 sum 如果不排序,去做汇总的话,一个大的session 运行起来会很慢 3.join控件 连接组件 对于异构的数据源,一个 ...
- Linux磁盘分区、挂载
⒈Linux下磁盘说明 1)Linux硬盘分IDE硬盘和SCSI硬盘,目前基本上是SCSI硬盘. 2)对于IDE硬盘,使用“hdx~”标识符,“hd”代表IDE硬盘. 对于SCSI硬盘,使用“sd ...
- Windows下 Robhess SIFT源码配置
Robhess OpenSIFT 源码下载:传送门 为了进一步学习SIFT,选择论文就着代码看,在VS2013.OpenCV2.4.13下新建项目,跑一跑经典之作.由于将代码和Opencv配置好后还会 ...
- 自助Linux之问题诊断工具strace【转】
转自:http://www.cnblogs.com/bangerlee/archive/2012/02/20/2356818.html 引言 “Oops,系统挂死了..." “Oops,程序 ...