【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. ...
随机推荐
- L1比L2更稀疏
1. 简单列子: 一个损失函数L与参数x的关系表示为: 则 加上L2正则化,新的损失函数L为:(蓝线) 最优点在黄点处,x的绝对值减少了,但依然非零. 如果加上L1正则化,新的损失函数L为:(粉线) ...
- 线程异步操作,更新其中一个报错不影响另一个的运行(Task )
//子系统同步更新 BD001_BLL bll = new BD001_BLL(); List<BD001_Model> lis ...
- TwemProxy Redis架构
TwemProxy 1.twemproxy是twitter开发的一个redis代理proxy. 通过Twemproxy可以使用多台服务器来水平扩张redis服务,可以有效的避免redis单点故障问题. ...
- 前端 - Ajax (1)
Ajax 主要作用 用于隐式提交,有别于input 提交时不会跳转/刷新页面. 前端: html 代码:(id) <p> <input id="user" typ ...
- Android的网络通信机制
1. Socket接口 不常用 2.HttpURLConnection接口 3. HttpClient接口 http://blog.csdn.net/ccc20134/article/details/ ...
- 【libreoffice】libreoffice实现office转pdf、html、jpg等格式数据
其实libreoffice有好多功能,完全可以替代office 1.windows下将word转为pdf 1 安装libreoffice 到官网下载后安装即可.https://donate.libr ...
- 【逆向工具】IDA使用4-控制台逆向分析 Reverse004.exe 获取密码
工具 吾爱破解版本OD.IDA6.8 OD使用-动态分析 OD快捷方式 F2 下断点,也就是指定断点的地址F3加载一个可执行程序,进行调试分析F4程序执行到光标处 F5 缩小.还原当前窗口 F7 单步 ...
- script & scriptreplay
script是什么 scirpt就是一个命令,可以制作一份记录输出到终端的记录.对于那些想要真实记录终端会话的人来说,这很有用.该记录可以保存并在以后再打印出来. 怎么用 默认情况下,我们可以通过在终 ...
- 为cobbler自动化安装系统工具添加epel源
关于cobbler的安装及部署,参考:CentOS 6.5自动化运维之基于cobbler服务的自动化安装操作系统详解http://blog.csdn.net/reblue520/article/det ...
- CentOS 6.5下的lamp环境rsyslog+MySQL+loganalyzer实现日志集中分析管理
前言 rsyslog系统日志,在CentOS5上叫syslog,而在CentOS6上叫rsyslog,是增强版的syslog,CentOS5上的配置文件在/etc/syslog.conf下,而Cent ...