【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard
More:【目录】LeetCode Java实现
Description
Similar to Question [Read N Characters Given Read4], but the read function may be called multiple times.
Intuition
题意:本题与上一题的区别就是连续多次调用read()函数,所以需要将存储4个字符的缓存buffer定义为全局变量,此外全局变量还需要定义buffer[]中的开始下标和缓存长度。本题中,需要注意的是,调用完一次read()函数后,可能没办法把buffer全部读完,所以要考虑到下一次调用read()函数时,对buffer的操作。详见代码。
Solution
- public class Solution extends Reader4 {
- private char[] buffer = new char[4];
- int offset = 0, bufsize = 0; //buffer[]中的开始下标和缓存长度
- /**
- * @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) {
- int readBytes=0; //已读的字符个数
- boolean eof=false;
- while(readBytes<n && !eof) {
- if(bufsize==0) { //buffer[]中没有缓存了
- bufsize=read4(buffer);
- eof=(bufsize<4); //不能放到外面!
- }
- int bytes=Math.min(bufsize, n-readBytes);
- System.arraycopy(buffer, offset, buf, readBytes, bytes);
- offset=(offset+bytes)%4;
- bufsize-=bytes;
- readBytes+=bytes;
- }
- return readBytes;
- }
- }
What I've learned
1.
More:【目录】LeetCode Java实现
【LeetCode】158. Read N Characters Given Read4 II - Call multiple times的更多相关文章
- 【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 158. Read N Characters Given Read4 II - Call multiple times 对一个文件多次调用read(157题的延伸题) --------- java
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- leetcode[158] Read N Characters Given Read4 II - Call multiple times
想了好一会才看懂题目意思,应该是: 这里指的可以调用更多次,是指对一个文件多次操作,也就是对于一个case进行多次的readn操作.上一题是只进行一次reandn,所以每次返回的是文件的长度或者是n, ...
- [leetcode]158. Read N Characters Given Read4 II - Call multiple times 用Read4读取N个字符2 - 调用多次
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- 158. Read N Characters Given Read4 II - Call multiple times
题目: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- [Locked] Read N Characters Given Read4 & Read N Characters Given Read4 II - Call multiple times
Read N Characters Given Read4 The API: int read4(char *buf) reads 4 characters at a time from a file ...
- 【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 ...
随机推荐
- MyBatis向数据库中批量插入数据
Foreach标签 foreach: collection:指定要遍历的集合; 表示传入过来的参数的数据类型.该参数为必选.要做 foreach 的对象,作为入参时,List 对象默认用 list 代 ...
- OpenCV入门(1)- 简介
1.图像的表示 在计算机看来,图像只是一些亮度各异的点,一副M*N的图片可以用M*N的矩阵来表示,矩阵的值表示这个位置上像素的亮度. 一般灰度图用二维矩阵来表示,彩色(多通道)图用三维矩阵表示,大部分 ...
- POJ1113 Wall【凸包】
题意: 求把城堡围起来需要的最小墙壁周长. 思路: 围墙周长为=n条平行于凸包的线段+n条圆弧的长度=凸包周长+围墙离城堡距离L为半径的圆周长. 代码: ...还是看大佬写的,自己做个记录方便日后复习 ...
- d 属性: 赋予字段执行动作的能力
1.对只读属性误解 property AppSetting: ISuperobject read fAppSetting;当看到 AppInfo.AppSetting.D['lastLat'] := ...
- 终端命令行开启和关闭mac隐藏文件
defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.apple.finde ...
- cdh部署
supermicro安装环境 本次安装基于无因特网的环境,共安装2个节点(一个master节点,一个data及节点),所用系统为centos7.x,所有安装过程均使用root用户.具体的节点信息如下: ...
- python魔法方法:__getattr__,__setattr__,__getattribute__
python魔法方法:__getattr__,__setattr__,__getattribute__ 难得有时间看看书....静下心来好好的看了看Python..其实他真的没有自己最开始想的那么简单 ...
- jvm系列三、java GC算法 垃圾收集器
原文链接:http://www.cnblogs.com/ityouknow/p/5614961.html 概述 垃圾收集 Garbage Collection 通常被称为“GC”,它诞生于1960年 ...
- WebSphere的jython编码的一个坑
was5.1版本,用"name=" in line这类判断字符串包含的方式时,系统会提示报错 TypeError: string member test needs char le ...
- docker在centos7系统镜像下遇到的一些问题
一.成功安装服务后发现无法启动 报错为:Failed to get D-Bus connection: Operation not permitted 系统为centos7官方版镜像,源和依赖之类的都 ...