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

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

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

  2. 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...

  3. 【LeetCode】1002. Find Common Characters 解题报告(Python)

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

  4. 【leetcode】1032. Stream of Characters

    题目如下: Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data ...

  5. 【leetcode】1002. Find Common Characters

    题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...

  6. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  7. 【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 ...

  8. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  9. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

随机推荐

  1. nmap学习之相关参数列表

    一.TARGET SPECIFICATION: Can pass hostnames, IP addresses, networks, etc. Ex: scanme.nmap.org, micros ...

  2. kubernetes 集群

    一.CentOS 7 基础环境准备 centos 默认服务目录 /usr/lib/systemd/system systemctl服务开机启动链接存贮目录: /etc/systemd/system/b ...

  3. [CQOI2011]放棋子 (DP,数论)

    [CQOI2011]放棋子 \(solution:\) 看到这道题我们首先就应该想到有可能是DP和数论,因为题目已经很有特性了(首先题面是放棋子)(然后这一题方案数很多要取模)(而且这一题的数据范围很 ...

  4. bzoj 4184: shallot (线段树维护线性基)

    题面 \(solution:\) 这一题绝对算的上是一道经典的例题,它向我们诠释了一种新的线段树维护方式(神犇可以跳过了).像这一类需要加入又需要维护删除的问题,我们曾经是遇到过的像莫对,线段树... ...

  5. Java 学习札记(三)免安装版TomCat中tomcat6w.exe的运行

    1.使用环境 很多时候我们用的是官网的解压免安装版的Tomcat,相比安装Tomcat除了少了安装步骤以外还少了tomcat6w.exe运行所需要的环境变量,所以一般Java开发免安装版的已经足够使用 ...

  6. HTML5实现全屏API【进入和退出全屏】

    现在主流浏览器基本上实现了全屏效果,但是不同浏览器实现不一样: [进入和退出全屏] // Webkit (works in Safari5.1 and Chrome 15)element.webkit ...

  7. mysql 查询优化 ~explain解读之type的解读

    一 简介:今天咱们来聊聊explain中type的相关解读 二 类型: system: 表中只有一条数据. 这个类型是特殊的 const 类型.  const: 针对主键或唯一索引的等值查询扫描, 最 ...

  8. Linux用户相关指令

    ⒈添加用户 ①useradd [Options] 用户名 useradd -d 指定用户目录 用户名 useradd -g 用户组 用户名 ⒉指定/修改用户密码 ①passwd 用户名 ⒊删除用户(建 ...

  9. scp -r拷贝目录不会拷贝软连接

    scp -r拷贝目录,不会拷贝 软连接的 解决方法: 使用rsync拷贝 参考:rsync本地及远程复制备份[原创] - paul_hch - 博客园 https://www.cnblogs.com/ ...

  10. mybatis主键自增长

    在数据库中建序列:SEQ_T_RESOURCE 第一种:会在传入该方法的的参数集合中添加主键元素,相当于:map.put("RES_ID",xxx); <insert id= ...