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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 【LeetCode】157. Read N Characters Given Read4

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...

  4. 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...

  5. [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 ...

  6. 利用spring boot创建java app

    利用spring boot创建java app 背景 在使用spring框架开发的过程中,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置和复杂的bean依赖关系,特别是在使用mvc的时候各 ...

  7. 利用eclipse新建的java web项目没有部署描述符web.xml文件怎么办?

    原文转自:http://blog.csdn.net/suyu_yuan/article/details/50947007 利用eclipse新建的Java Web项目没有部署描述符web.xml文件, ...

  8. 解析android framework下利用app_process来调用java写的命令及示例

    解析android framework下利用app_process来调用java写的命令及示例 在android SDK的framework/base/cmds目录下了,有不少目录,这些目的最终都是b ...

  9. 如何利用JConsole观察分析Java程序的运行并进行排错调优_java

    如何利用JConsole观察分析Java程序的运行并进行排错调优_java 官方指导  use jconsole use jmx technology

随机推荐

  1. 初试 Matlab 之去除水印

    这几天很痛苦地去学习了下用 Matlab 来处理图像,其实那些算法我觉得还不算很难理解,可是 Matlab 这种反人类的语法(可能对于我来说是这样吧,毕竟熟悉了 C++ / Java 的语法一时间很难 ...

  2. Decorator

    1 意图:动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更灵活. 2 别名:包装器Wrapper 3 动机:将组件嵌入到另一个对象中,由这个对象添加边框.嵌入的 ...

  3. Security » Authorization » 介绍

    Introduction¶ 介绍 77 of 87 people found this helpful Authorization refers to the process that determi ...

  4. Gulp Babel AMD转换例子

    1.gulpfile.js var gulp = require('gulp'); const babel = require('gulp-babel'); gulp.task('default', ...

  5. /etc/rc.d/与/etc/rc.d/init.d的关系

    /etc/init.d指向/etc/rc.d/init.d目录 . 除了直接调用脚本外(如/etc/rc.d/init.d/xinetd),还可以用service命令来控制init.d目录下的服务如 ...

  6. Java—类的封装、继承与多态

    一.类和对象 1.类 类是数据以及对数据的一组操作的封装体. 类声明的格式: 类声明 { 成员变量的声明: 成员方法的声明及实现: } 1.1 声明类 [修饰符] class 类<泛型> ...

  7. c#网络通信框架networkcomms内核解析之十 支持优先级的自定义线程池

    NetworkComms网络通信框架序言 本例基于networkcomms2.3.1开源版本  gplv3协议 如果networkcomms是一顶皇冠,那么CommsThreadPool(自定义线程池 ...

  8. ios中autolayout

    IOS 6 自动布局 入门-1  Matthijs Hollemans on September 29, 2012 Tweet 这篇文章还可以在这里找到 英语, 韩语, 土耳其语 If you're ...

  9. XML 链接

    公共Webservice   网络上可供测试的Web Service腾讯QQ在线状态 WEB 服务Endpoint: http://www.webxml.com.cn/webservices/qqOn ...

  10. ABAP遇到的问题——1

    在创建ABAP对象的时候抛出“测试对象不能被创建在外来命名空间”的错误 原因:程序的名字不是以Z或者Y开头的.