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

  1. 边工作边刷题:70天一遍leetcode: day 89

    Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...

  2. 边工作边刷题:70天一遍leetcode: day 77

    Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...

  3. 边工作边刷题:70天一遍leetcode: day 78

    Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...

  4. 边工作边刷题:70天一遍leetcode: day 85-3

    Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...

  5. 边工作边刷题:70天一遍leetcode: day 101

    dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...

  6. 边工作边刷题:70天一遍leetcode: day 1

    (今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...

  7. 边工作边刷题:70天一遍leetcode: day 70

    Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...

  8. 边工作边刷题:70天一遍leetcode: day 71-3

    Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...

  9. 边工作边刷题:70天一遍leetcode: day 71-2

    One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...

随机推荐

  1. eclipse中的web项目路径和发布好的项目路径

    现在企业开发中,我们都会创建一个javaWeb工程,在eclipse中指的是新建一个dynamic web project,创建完工程之后,我们在IDE中大体看到如下的工程目录: 我们主要关心的文件夹 ...

  2. Mybatis学习记录(五)----Mybatis的动态SQL

    1.  什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.1 需求 用户信息综合查询列表和用户信息查询列表总数这两个statemen ...

  3. SharePoint 2010 External List Paging – Server Side

    http://lightningtools.com/bcs/sharepoint-2010-external-list-paging-server-side/ When you are using a ...

  4. 数组拷贝 copyOf()

    Arrarys类的copyof方法与copyOfRange方法可以实现对数组的复制,前者是复制数组到指定的长度,后者将指定的长度复制到一个新数组中. 1.copyOf()方法 该方法提供了很多种重载形 ...

  5. AsyncTask.cancel()的结束问题

    实际项目中有这么一个问题,用户进入详情界面,那么我们就要网络加载数据并展现在UI上,这个加载用线程或者异步. 这里就拿项目中统一用异步任务来获取网络数据把. 用户可能会有这么一个操作,它在一个商品(说 ...

  6. Android代码混淆官方实现方法

    首先查看一下 “project.properties” 这个文件: # This file is automatically generated by Android Tools.# Do not m ...

  7. 错误:升级为xcode8之后无法上网的解决方法

    主要是在info.list中增加以下的节点,因为XCode开始所有的http都转为https来联网了. 添加类型为Dictionary的:NSAppTransportSecurity 再添加节点为:N ...

  8. IOS 登陆界面的简单编写(通过NSNotificationCenter)

    在介绍内容的之前,先看一下实现结果. 通过细节可以发现,只有当手机号与密码都输入的情况登录按钮才会变亮.那么这是怎么实现的呢? 首先我们要知道,这种情况的发生的首要条件便是每时每刻都知道两个TextF ...

  9. iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄

    前言:本篇讲解,在前篇iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载的基础上,使用输出流代替文件句柄实现大文件断点续传.    在实际开发中,输入输出流用的比较少,但 ...

  10. 基于WF4.0的公文管理系统

    系统功能说明 公文管理 通过定义公文的基本信息,并将它按照工作流的定义流转实现公文的管理.包含以下功能: )公文创建:用户能够将格式化文本作为公文上传到系统中,并选择工作流启动流程. )公文审批:具有 ...