亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

158. Read N Characters Given Read4 II - Call multi

SillyMonkey / 2457人閱讀

摘要:題目鏈接和那道不同的是這次,問題就是當(dāng)前的可能存在多讀了幾個(gè)字節(jié),那么下一次的時(shí)候要先算上上次多讀的部分,所以要保存上次讀的。和讀一次一樣有兩種要考慮的讀完了沒讀完,但是裝滿了

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

題目鏈接:https://leetcode.com/problems...

和那道read n不同的是這次call multiple times,問題就是當(dāng)前的call可能存在多讀了幾個(gè)字節(jié),那么下一次call read的時(shí)候要先算上上次多讀的部分,所以要保存上次讀的。和讀一次一樣有兩種要考慮的case:

file讀完了: read4(buf[]) == 0

file沒讀完,但是buf裝滿了read4(buf[]) > n - res

public class Solution extends Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Maximum number of characters to read
     * @return    The number of characters read
     */
    char[] buff = new char[4];
    int start = 0;
    int num = 0;
    public int read(char[] buf, int n) {
        int res = 0;
        while(res < n) {
            // no previous buff remain
            if(start == 0) num = read4(buff);
            // finish reading
            if(num == 0) break;
            // count the remain char to use for next call: start is the next start
            while(res < n && start < num) buf[res++] = buff[start++];
            // clear start if read all: n - res >= num - start
            if(start == num) start = 0;
        }
        return res;
    }
}
157. Read N Characters Given Read4
    public int read(char[] buf, int n) {
        int res = 0;
        char[] temp = new char[4];
        while(res < n) {
            int cur = read4(temp);
            if(cur == 0) break;
            
            int num = Math.min(cur, n - res);
            for(int j = 0; j < num; j++) buf[res++] = temp[j];
        }
        return res;
    }

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/76407.html

相關(guān)文章

  • [LeetCode] 158. Read N Characters Given Read4 II -

    Problem 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 i...

    周國輝 評(píng)論0 收藏0
  • [Leetcode] Read N Characters Given Read4 用Read4 AP

    摘要:臨時(shí)數(shù)組法復(fù)雜度時(shí)間空間思路用一個(gè)臨時(shí)數(shù)組,存放每次讀到字符,再用一個(gè)指針標(biāo)記數(shù)組目前存儲(chǔ)到的位置,然后將這個(gè)臨時(shí)數(shù)組的內(nèi)容存到相應(yīng)的位置就行了。 Read N Characters Given Read4 I The API: int read4(char *buf) reads 4 characters at a time from a file. The return valu...

    wayneli 評(píng)論0 收藏0
  • 157. Read N Characters Given Read4

    摘要:題目解答讀懂題目很重要,還是要多寫寫這種實(shí)際的的問題。實(shí)際文件讀到頭了的情況需要讀的文件個(gè)數(shù)達(dá)到了的情況 題目: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 exam...

    crossoverJie 評(píng)論0 收藏0
  • 翻轉(zhuǎn)字符串的相關(guān)題目

    摘要:一題目描述空格分隔,逐個(gè)反轉(zhuǎn)二題目描述三題目描述當(dāng)然也可以用的做,不過用雙指針更快。 LeetCode: 557. Reverse Words in a String III 一、LeetCode: 557. Reverse Words in a String III 題目描述 Given a string, you need to reverse the order of chara...

    lykops 評(píng)論0 收藏0
  • 【LC總結(jié)】圖、拓?fù)渑判?(Course Schedule I, II/Alien Dictiona

    Course Schedule Problem There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, whi...

    gaara 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<