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. js 布局转换问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. linq to xml 简单的增、删、改、查、保存xml文件操作

    using System; using System.Collections; using System.Configuration; using System.Data; using System. ...

  3. string字符串js操作

    String 对象方法 方法 描述 anchor() 创建 HTML 锚. big() 用大号字体显示字符串. blink() 显示闪动字符串. bold() 使用粗体显示字符串. charAt() ...

  4. Java EE之Form的get与post方法

    Form表单中method="post/get'的区别 Form提供了两种数据传输的方式——get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生 ...

  5. HTML5 CSS JavaScript在网页中扮演的角色

    HTML (content layer) CSS (presentation layer) JavaScript (Interactive layer) 参考链接: [1] https://www.y ...

  6. mysql 原理 ~ binlog

    一 简介:我们会持续对binlog进行分析,但是不深入代码二 版本 5.6    格式    GTID和传统格式    传统格式     一 binlog针对具体事务注意点-1         1 u ...

  7. ARMV8 datasheet学习笔记3:AArch64应用级体系结构

    1.前言 本文主要从应用的角度介绍ARMV8的编程模型和存储模型 2. AArch64应用级编程模型 从应用的角度看到的ARM处理器元素: 可见的元素(寄存器/指令) 说明 可见的寄存器 R0-R30 ...

  8. git入门与实践【转】

    转自:http://www.cnblogs.com/shenhaocn/archive/2011/03/13/1982957.html 什么是版本控制 要了解什么是git,首先需要了解什么是版本控制( ...

  9. 连接Linux服务器操作Oracle数据库

    连接Linux服务器操作Oracle数据库   由于项目已经上线,现场的数据库服务器不允许直接用Oracle的客户端plsqldev.exe来连接,只能通过Linux服务器的命令来操作. 以下是用Se ...

  10. Hyper-V虚拟机上安装一个图形界面的Linux系统

    这件事情呢,一直想干但又觉得太陌生和麻烦,无奈现在到了非装不可的时候,只好硬着头皮去装.在此之前,我不懂什么叫做虚拟机,linux也接触甚少.经过3天的折腾,终于装好了带有图形界面的linux(字符版 ...