✡ 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
随机推荐
- shell学习笔记1
知识点1.修改输出的颜色 echo -e "\e[1;31m This is red text \e[0m" 其中,\e[1;31m 表示将输出显示为红色: \e[0m 标识将颜色 ...
- 手机测试pc端网页
在这个问题上徘徊了 一个钟头了,终于被我找到方法了,就赶紧记下来,以后好查阅!! 主要问题在防火墙,防火墙阻当了80端口,所以怎么用手机访问都是访问不了的.把防火墙关闭就好了! 贴上httpd-vho ...
- C语言中常见的排序方法
在C语言中,常见的排序方法有冒泡法,排序法,插入法等等.所谓的冒泡法,就是对一组数字进行从大到小或者从小到大的一种排序方法.主要就是相邻的数值相互交换.从第一个数值开始,如果这相邻的两个数值排序与我们 ...
- 关于php语言的使用! ------php语言与JavaScript的使用 方法是相似
<script type="text/javascript"> </script>--js与PHP同是一种弱类型语言 弱类型语言只是不显示表现 定义变量时 ...
- ASP.Net Web.config 中引用外部config文件
1. 前提准备: Web.config file: <?xml version="1.0" encoding="utf-8"?><config ...
- CAD打开文件总是弹出要求选择字体怎么办
CAD打开文件总是弹出要求选择字体怎么办1.在C:\Documents and Settings\下搜索acad.fmp文件,双击用记事本打开acad.fmp文件,在最后添加内容,上面几行本来就有,不 ...
- Centos7下搭建LAMP平台环境 (转载)
1.启用Apache(httpd) Centos7默认已经安装httpd服务,只是没有启动.如果你需要全新安装,可以yum install -y httpd 启动服务:systemctl start ...
- html知识2
1.超链接 语法:<a href "" target="打开方式" name="页面锚点名称">链接文字或者图片</a&g ...
- 自定义N维空间数组
class Space : IEnumerable<Space> { public object Filler { get { return filler ?? (filler = Top ...
- 无障碍网页设计(WCAG2.0)
无障碍化是指无论健全人还是残疾人,年轻人还是老年人都可以平等地获取互联网上的信息和服务.无障碍化网站建设不单可以增加网站的受益群体,更是一个有情怀.有担当的互联网企业的责任,也是一个互联网从业人员应该 ...