边工作边刷题:70天一遍leetcode: day 73
Read N Characters Given Read4 I/II
要点:这题的要点就是搞清楚几个变量的内在逻辑:只有buffer是整4 bytes的。而client要读的bytes(需求)和实际上disk上有的bytes(供给)都是不整的。所以,
- 循环的条件就是either 供给不足 or 需求不足:供给不足的判定是上一轮数据不够4 bytes的mark,而需求不足是toRead的计数<=0
- 所以循环体内,就是min(读进来的bytes, toRead)来把数据copy到buffer里,同时更新toRead和结束条件
- II就是加了个buffer来存上一轮的leftover和offset,在下一次读的时候,把剩余数据假装作为一个read4来处理。
- 为什么要用bufSize而不是offset本身?offset表示的是数据的起始位置(当前位置是还没读的),可能有数据,也可能没有。
- 所以逻辑是只有bufSize==0才读4 bytes,global buffer做缓冲区,而bufSize永远标示待读区间的大小
- offset不断从0向右,然后再回到0:4 bytes肯定一次读进来
I: https://repl.it/Cjjw/1
II: https://repl.it/CjkR/2
错误点:
- self.offset>=4,不是>4
- 别忘了eof(在bufsize==0里面)
# 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.
# Hide Company Tags Facebook
# Hide Tags String
# Hide Similar Problems (H) Read N Characters Given Read4 II - Call multiple times
# The read4 API is already defined for you.
# @param buf, a list of characters
# @return an integer
# def read4(buf):
class Solution(object):
def read(self, buf, n):
"""
:type buf: Destination buffer (List[str])
:type n: Maximum number of characters to read (int)
:rtype: The number of characters read (int)
"""
eof, nbytes = False, 0
while not eof and nbytes<n:
buf4 = [""]*4
nread = read4(buf4)
if nread<4:
eof = True
nnext = min(nread, n-nbytes)
for i in xrange(nnext):
buf[nbytes+i]=buf4[i]
nbytes+=nnext
return nbytes
# 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 may be called multiple times.
# Hide Company Tags Bloomberg Google Facebook
# Hide Tags String
# Hide Similar Problems (E) Read N Characters Given Read4
# The read4 API is already defined for you.
# @param buf, a list of characters
# @return an integer
# def read4(buf):
class Solution(object):
def __init__(self):
self.bufbytes, self.offset = 0,0
self.buf4 = [""]*4
def read(self, buf, n):
"""
:type buf: Destination buffer (List[str])
:type n: Maximum number of characters to read (int)
:rtype: The number of characters read (int)
"""
eof, nbytes = False, 0
while not eof and nbytes<n:
if self.bufbytes==0:
self.bufbytes = read4(self.buf4)
if self.bufbytes<4: # error: don't forget
eof = True
toread = min(n-nbytes, self.bufbytes)
#print toread,self.offset
for i in xrange(toread):
buf[nbytes+i]=self.buf4[self.offset+i]
self.offset+=toread
if self.offset >= 4: # error: >=4, last index is 3
self.offset-=4
self.bufbytes-=toread
nbytes+=toread
return nbytes
边工作边刷题:70天一遍leetcode: day 73的更多相关文章
- 边工作边刷题:70天一遍leetcode: day 89
Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- 边工作边刷题:70天一遍leetcode: day 101
dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
- 边工作边刷题:70天一遍leetcode: day 71-3
Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...
- 边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...
随机推荐
- 探索HashMap实现原理及其在jdk8数据结构的改进
因为网上已经太多的关于HashMap的相关文章了,为了避免大量重复,又由于网上关于java8的HashMap的相关文章比较少,至少我没有找到比较详细的.所以才有了本文. 本文主要的内容: 1.Hash ...
- 代码导出Reporting Services报表文件
背景部分 使用Reporting Services很容易制作和发布我们需要的报表,报表效果也还不错 不过如果报表数据过大或报表数量过多,打开及查看报表感觉可能就是另外一回事了 好在Reporting ...
- SparseArray<E>详解
SparseArray<E> 是官方推荐的用来替代 HashMap<Integer, E> 的一个工具类,相比来说有着更好的性能(其核心是折半查找函数(binarySearch ...
- Mac下利用(xcode)安装git
Mac下利用(xcode)安装git 一.AppStore 最安全途径:搜索下载Xcode,(需要AppleID). 其他:直接百度Xcode下载. 二.Xcode 打开Xcode-->Pref ...
- 使用CocoaPods管理第三方开源类库
iOS开发中经常会用到许多第三方开源类库,比如AFNetworking.FMDB.JSONKit等等,使用CocoaPods这个工具就能很方便得对工程中用到的类库进行管理,包括自动下载配置以及更新. ...
- Swift 中的函数
学习来自<极客学院:Swift中的函数> 工具:Xcode6.4 直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会 练习一: import Foundation //函数 ...
- 万恶的hao123
Windows 10没办法直接在系统菜单栏上修改快捷图标的参数 在确认系统里面没有流氓软件之后,只能手工到文件夹下去修改了 C:\Users\你的用户名\AppData\Roaming\Microso ...
- mysql登录和连接 权限
在一些配置中会要求登录mysql 授权的时候注意ip地址是ip地址,localhost是localhost,在grant授权时,如果用localhost,就必须在所登录的配置文件中使用localhos ...
- rails关于utf8问题-------------------utf8申明必须置顶
utf-8必须置顶,如果放在其他位置,会导致后面如果遇到中文无法解析,然后报其他乱七八糟的错误,比如不能连接数据库,比如语法错误......这种错误不好找,切记!!! 出错代码: #!/bin/env ...
- find locate
locate执行前先 updatedb 然后locate vstore 就可以了 find 加 -name 比如 find -name vstore 按理说 locate要快点,毕竟是数据库嘛 一:l ...