码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 158: Read N Characters Given Read4 II

时间:2017-09-05 14:36:31      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:public   parent   des   max   character   tar   ade   efi   ext   

/* The read4 API is defined in the parent class Reader4.
      int read4(char[] buf); */

public class Solution extends Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Maximum number of characters to read
     * @return    The number of characters read
     */
    private int prevStart = 0;
    private int prevEnd = 0;
    private char[] buffered = new char[4];
    public int read(char[] buf, int n) {
        int currentStart = 0;
        while (currentStart < n) {
            if (prevStart == 0) {
                prevEnd = read4(buffered);
            }
            if (prevEnd == 0) {
                break;
            }
            while (currentStart < n && prevStart < prevEnd) {
                buf[currentStart++] = buffered[prevStart++];
            }
            if (prevStart >= prevEnd) {
                prevStart = 0;
            }
        }
        return currentStart;
    }
}

 

LeetCode 158: Read N Characters Given Read4 II

标签:public   parent   des   max   character   tar   ade   efi   ext   

原文地址:http://www.cnblogs.com/shuashuashua/p/7478355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!