首页 > 其他 > 详细

158. Read N Characters Given Read4 II - Call multiple times

时间:2018-08-17 01:01:17      阅读:181      评论:0      收藏:0      [点我收藏+]
158. Read N Characters Given Read4 II - Call multiple times




Given buf = "abc"
read("abc", 1) // returns "a"
read("abc", 2); // returns "bc"
read("abc", 1); // returns ""
Example 2: 

Given buf = "abc"
read("abc", 4) // returns "abc"
read("abc", 1); // returns ""

   

    private int buffPtr = 0;
    private int buffCnt = 0;
    private char[] buff = new char[4];
    public int read(char[] buf, int n) {
        int ptr = 0;
        while (ptr < n) {
            if (buffPtr == 0) {
                buffCnt = read4(buff);
            }
          
            if (buffCnt == 0) break;
          
            while (ptr < n && buffPtr < buffCnt) {
                buf[ptr++] = buff[buffPtr++];
            }
          
            if (buffPtr >= buffCnt){
              buffPtr = 0;
            }
        }
        return ptr;
    }
I used buffer pointer (buffPtr) and buffer Counter (buffCnt)
  to store the data received
in previous calls. In the while loop,
if buffPtr reaches current buffCnt, it will be set
as zero to be ready to read new data.

 

158. Read N Characters Given Read4 II - Call multiple times

原文:https://www.cnblogs.com/tobeabetterpig/p/9490943.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!