【LeetCode】157. Read N Characters Given Read4
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的更多相关文章
- 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
- 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
- 【LeetCode】1002. Find Common Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【leetcode】1032. Stream of Characters
题目如下: Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data ...
- 【leetcode】1002. Find Common Characters
题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【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 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
随机推荐
- java实现网页验证码
Servlet: package cn.bdqn.servlet; import javax.imageio.ImageIO; import javax.servlet.ServletExceptio ...
- 20155332 2006-2007-2 《Java程序设计》第4周学习总结
20155332 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 理解封装.继承.多态的关系 理解抽象类与接口的区别 掌握S.O.L.I.D原则 了解模式和设 ...
- Linux - 账户切换授权
sudo 切换账户 echo myPassword | sudo -S ls /tmp # 直接输入sudo的密码非交互,从标准输入读取密码而不是终端设备 visudo # sudo命令权限添加 /e ...
- tomcat server.xml
基于对server.xml的学习,结合源码,可以进一步理解tomcat的架构设计 1. 2. 3. 4 .valve链 参考: http://www.importnew.com/17124.html ...
- 列式数据库~clickhouse 底层存储原理
简介:今天介绍列式数据库的一些基本原理 一 数据目录 Data目录 数据存储目录,数据按照part分成多个文件夹,每个文件夹下存储相应数据和对应的元信息文件 Metadata 表定义语句,存储所有表 ...
- TDateTimePicker 选择最小日期时异常处理
TDateTimePicker 控件属性窗体选择最小日期,运行时选择时可以看到的最小的日期,但是选择最小时就异常 :date is less than minimum of *** 解决过程 ...
- SpringBoot启动方式讲解和部署war项目到tomcat9
1.SpringBoot启动方式讲解和部署war项目到tomcat9简介:SpringBoot常见启动方式讲解和部署war项目Tomcat 1.ide启动 2.jar包方式启动 maven插件: &l ...
- Shiro会话管理器与验证码实现(十四)
和shiro整合后,使用shiro的session管理,shiro提供sessionDao操作 会话数据. 配置sessionManager
- JavaScript内置对象——Math对象
这几天在刷leetcode的时候用到了一些Math对象的知识,故作一下总结~ JavaScript中的Math对象也是一个常见的内置对象,然而与String等其它常见对象不同,Math对象没有构造函数 ...
- windows上python上传下载文件到linux服务器指定路径【转】
从windows上传文件到linux,目录下的文件夹自动创建 #!/usr/bin/env python # coding: utf-8 import paramiko import datetime ...