✡ 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
随机推荐
- Android ListView简单实用
layout创建: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ ...
- (转载)TCP/IP的三次握手与四次挥手
TCP三次握手 所谓三次握手(Three-way Handshake),是指建立一个TCP连接时,需要客户端和服务器总共发送3个包. 三次握手的目的是连接服务器指定端口,建立TCP连接,并同步 ...
- sqlalchemy 优化count()……
一.sqlalchemy 中的count() count()统计数据特别慢: session.query(cls).count() 8W 数据花费了近50s 但是在数据库中直接查询: select ...
- 在caffe中使用hdf5的数据
caffe默认使用的数据格式为lmdb文件格式,它提供了把图片转为lmdb文件格式的小程序,但是呢,我的数据为一维的数据,我也要分类啊,那我怎么办?肯定有办法可以转为lmdb文件格式的,我也看了一些源 ...
- Deep Learning 17:DBN的学习_读论文“A fast learning algorithm for deep belief nets”的总结
1.论文“A fast learning algorithm for deep belief nets”的“explaining away”现象的解释: 见:Explaining Away的简单理解 ...
- Head First 设计模式 --7 适配器模式 外观模式
适配器模式:将一个类东街口转换成客户期望的另一个接口.适配器让原本接口不兼容的类可以合作无间. 适配器模式有两种,对象适配器和类的适配器.先看一下对象适配器. 还是看最开始鸭子的例子,如果此时鸭子不够 ...
- BWT (Burrows–Wheeler_transform)数据转换算法
1.什么是BWT 压缩技术主要的工作方式就是找到重复的模式,进行紧密的编码. BWT(Burrows–Wheeler_transform)将原来的文本转换为一个相似的文本,转换后使得相同的字符位置连续 ...
- python核心编程第六章练习6-15
转换.(a)给出两个可识别格式的日期,比如MM/DD/YY或者DD/MM/YY格式.计算出两个日期之间的天数.(b)给出一个人的生日,计算此人从出生到现在的天数,包括所有的闰月.(c)还是上面的例子, ...
- JavaScript数组API
1.将数组转化为字符串:2种: 1.var str=String(str); 将数组转化为字符串并分隔每个元素 ...
- 【转】XGBoost参数调优完全指南(附Python代码)
xgboost入门非常经典的材料,虽然读起来比较吃力,但是会有很大的帮助: 英文原文链接:https://www.analyticsvidhya.com/blog/2016/03/complete-g ...