✡ leetcode 157. Read N Characters Given Read4 利用read4实现read --------- java
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.
read4(char[] buf)指的是读取4个数,存储在buf中,然后返回成功读取的数目,如果不够四个,那么就返回剩余数目的字符。
实现的read(char[] buf, int n)功能类似,只不过将4改为了n,需要输入而已。(这里一个例子只会读取一次read)
刚开始做的时候理解错了,以为是读取buf中的字符,导致提交失败。
方法是:先读取n/4次read4,如果期间有出现了不等于4的情况,那么返回结果。
然后读取最后一次,需要判断的是:1、如果刚好读完了,直接返回结果
2、判断n-result和读取数目num的大小,选择小的,读取并返回数目。
/* 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) {
if (n < 1){
return 0;
}
int time = n / 4;
int result = 0;
char[] chars = new char[4];
for (int i = 0; i < time; i++){
int num = read4(chars);
for (int j = 0; j < num; j++){
buf[i * 4 + j] = chars[j];
}
if (num != 4){
result += num;
return result;
} else {
result += 4;
}
}
if (n - result == 0){
return result;
}
int num = read4(chars);
for (int i = 0; i < Math.min(n - result, num); i++){
buf[result + i] = chars[i];
}
result += Math.min(n - result, num);
return result;
}
}
✡ leetcode 157. Read N Characters Given Read4 利用read4实现read --------- java的更多相关文章
- [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
Problem: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is ...
- 【LeetCode】157. Read N Characters Given Read4
Difficulty: Easy More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...
- 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
- [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 ...
- 利用spring boot创建java app
利用spring boot创建java app 背景 在使用spring框架开发的过程中,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置和复杂的bean依赖关系,特别是在使用mvc的时候各 ...
- 利用eclipse新建的java web项目没有部署描述符web.xml文件怎么办?
原文转自:http://blog.csdn.net/suyu_yuan/article/details/50947007 利用eclipse新建的Java Web项目没有部署描述符web.xml文件, ...
- 解析android framework下利用app_process来调用java写的命令及示例
解析android framework下利用app_process来调用java写的命令及示例 在android SDK的framework/base/cmds目录下了,有不少目录,这些目的最终都是b ...
- 如何利用JConsole观察分析Java程序的运行并进行排错调优_java
如何利用JConsole观察分析Java程序的运行并进行排错调优_java 官方指导 use jconsole use jmx technology
随机推荐
- monkey中的一些问题
一起来看下导致App Crash的那些原因: 1. 空指针异常:错误日志定位java.lang.NullPointerException,详细日志记录如下 2. 安全异常:错误日志定位 ...
- zoj3228Searching the String(ac自动机)
链接 这个题把病毒分为了两种,一种包含可以覆盖,另一种不可以,需要分别求出包含他们的个数,可以把两种都建在一颗tire树上,在最后求得时候判断一下当前节点是属于哪种字符串,如果是不包含的需要判断一下p ...
- Spring Boot Admin的使用
http://www.jianshu.com/p/e20a5f42a395 ******************************* 上一篇文章中了解了Spring Boot提供的监控接口,例如 ...
- [SoapUI] SoapUI Response 格式控制
application/后面可以修改为自己所需要的内容格式.
- php支付宝在线支付接口开发教程【转】
php支付宝在线支付接口开发教程 这篇文章主要为大家详细介绍了php支付宝在线支付接口开发教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 1.什么是第三方支付 所谓第三方支付,就是一些和各 ...
- C++类的交叉引用
对于C++中,两个类中相互引用对方,当然只能是在指针的基础上,于是我们知道.也就是说在A类的有一个指针引用B类的成员函数或成员对象,而B类中又有一个指针来访问A中的成员函数或对象.这就是C++中类的交 ...
- Xcode集成开发环境的安装
Xcode是苹果官方提供的iOS开发环境,安装方式如下: 安装过程需要登录AppleID账号,如果你没有苹果账号,可以免费注册一个.根据提示就能完成Xcode安装.
- 0517 Scrum 项目4.0
Sprint 1看板: 任务认领(一种标签颜色代表一个人的任务): 许佳仪:紫色 柯晓君:红色 赖文亮:蓝色 卓宇靖:黑色 每日例会的时间:16:00 地点:课室 成员 团队贡献分 许佳仪 1 ...
- WPF CAL 计算器
界面最终结果: 下载地址:https://skydrive.live.com/redir?resid=25C3908AA2038BDB!148&authkey=!ADR71XdB04LipYE
- PHP数组去重..............过滤字段
$test_data = M('hot'); //实例化数据表 $data = $test_data->Distinct(true)->field('descriprion')->o ...