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, 0while not eof and nbytes<n:buf4 = [""]*4nread = read4(buf4)if nread<4:eof = Truennext = min(nread, n-nbytes)for i in xrange(nnext):buf[nbytes+i]=buf4[i]nbytes+=nnextreturn 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,0self.buf4 = [""]*4def 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, 0while not eof and nbytes<n:if self.bufbytes==0:self.bufbytes = read4(self.buf4)if self.bufbytes<4: # error: don't forgeteof = Truetoread = min(n-nbytes, self.bufbytes)#print toread,self.offsetfor i in xrange(toread):buf[nbytes+i]=self.buf4[self.offset+i]self.offset+=toreadif self.offset >= 4: # error: >=4, last index is 3self.offset-=4self.bufbytes-=toreadnbytes+=toreadreturn nbytes

转载于:https://www.cnblogs.com/absolute/p/5815662.html

边工作边刷题:70天一遍leetcode: day 73相关推荐

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

    Largest BST Subtree 要点: http://articles.leetcode.com/largest-binary-search-tree-bst-in 这题重点是理解题意,还有道 ...

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

    Gas Station 老题,这种circular的题一般都能转化成单向的.比如这题就是用sumDiff来判断是否有解,而start单向递增. class Solution(object):def c ...

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

    House Robber I/II/III 这题代表了单向线性dp的基本pattern: build local best value at each element and track the gl ...

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

    LRU Cache 这是一道leetcode的难题,这种题往往是算法结构很复杂,涉及一个或多个考点算法和数据结构的组合,同时又有很多corner cases要考虑.所以一定要找到合适memorize的 ...

  5. 边工作边刷题:70天一遍leetcode: day 97-2

    Design Hit Counter 要点:因为是second granularity,所以可以用以秒为单位的circular buffer方法.这题简单在只需要count过去300秒的,增加难度可以 ...

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

    Max Points on a Line 要点:这题暴力解是用任何两点确定一条直线,然后对其他点检查是否共线,显然,这里没用空间来存储之前的检查结果,所以time complexity是O(n^3). ...

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

    Rectangle Area 要点:基本思路就是先分开算再减去相交部分,这题的难点是如何检查是否相交和如何算出相交部分的面积. 2d转化为1d:x轴和y轴是orthogonal的.可以分开考虑.这样检 ...

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

    Compare Version Numbers 题本身思路简单没什么可说的.值得一提的是corner case:多出来的0和没有是相同版本,这样最简单的处理是直接对没有的补0.这样因为需要补齐,循环是 ...

  9. 边工作边刷题:70天一遍leetcode: day 11-1

    Clone Graph dfs或者bfs都可以做,这题的要点是hashmap有两个作用:一是图遍历中的visited,另一个是存copy的结点来连接neighbors.hashmap即表示visite ...

最新文章

  1. msdn画圆弧函数_复变函数与积分变换 简明笔记(八):保形映射(共形映射)
  2. Python实现tab文件操作
  3. java 内存映射文件进程间通讯_[转]Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile...
  4. list集合去重的三种方式
  5. c 获得java数据_JNI:如何将一组数据从c转换为Java
  6. html5测试题整理--针对标签的概念性
  7. Python+selenium第一个自动化脚本
  8. 动态规划在求解传递闭包问题中的应用(JAVA)--Warshell算法
  9. Oracle 安装时候的fs.aio-max-nr参数
  10. c语言是否继续运行,C语言是否有运行时?
  11. vue锚链接可以从指定位置显示么_onenote链接系列⑥:链接笔记如何产生?与插入链接的区别...
  12. java并发:线程同步机制之计数器Exechanger
  13. Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)
  14. python读取文件夹下所有图像_Python 读取指定文件夹下的所有图像方法
  15. SliderBar4.0常用滑块滚动效果封装
  16. jasperprint java_jasperReport的一些参数
  17. 在win10中加载ISO文件到虚拟光驱
  18. 工作中遇到的小技巧 一(暂停更新)
  19. jvm原理与性能调优
  20. 特征值(eigenvalue)特征向量(eigenvector)特征值分解(eigenvalue decomposition)

热门文章

  1. 【POJ - 2909 】Goldbach's Conjecture (哥德巴赫猜想,数论,知识点结论)
  2. 【POJ - 1001 】Exponentiation (Java大数,高精度)
  3. c语言 单词变复数_一些复数运算的C语言实现
  4. 获取10~99(包含10和99)的“总和”与“偶数”的个数
  5. java 截串_java字符串截取
  6. 子窗体中组合框联动_一张表实现组合框联动
  7. PHP获取服务器端的相关信息
  8. (acm)C++加速输入的几种方法
  9. 外挂编程-动作模拟技术
  10. java es 数据批量导入_ElasticSearch—Java批量导入导出