成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

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

周?chē)?guó)輝 / 1027人閱讀

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

Example 1:

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 ""
Solution
public class Solution extends Reader4 {
    /**
     * @param buf Destination buffer
     * @param n   Maximum number of characters to read
     * @return    The number of characters read
     */
    int count = 0;  //count the length of data retrieved by read4()
    int index = 0;  //record the position of data consumed by read()
    char[] data = new char[4];
    public int read(char[] buf, int n) {
        int i = 0;
        while (i < n) {
            //get new data with read4 api
            if (index == 0) count = read4(data);
            //if no new data, break
            if (count == 0) break;
            //consume data
            while (i < n && index < count) {
                buf[i++] = data[index++];
            }
            //all existing data consumed, restart with index = 0
            if (index >= count) index = 0; 
        }
        //return the length of data read
        return i;
    }
}

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

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

相關(guān)文章

  • 158. Read N Characters Given Read4 II - Call multi

    摘要:題目鏈接和那道不同的是這次,問(wèn)題就是當(dāng)前的可能存在多讀了幾個(gè)字節(jié),那么下一次的時(shí)候要先算上上次多讀的部分,所以要保存上次讀的。和讀一次一樣有兩種要考慮的讀完了沒(méi)讀完,但是裝滿了 158. Read N Characters Given Read4 II - Call multiple times 題目鏈接:https://leetcode.com/problems... 和那道read...

    SillyMonkey 評(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

    摘要:題目解答讀懂題目很重要,還是要多寫(xiě)寫(xiě)這種實(shí)際的的問(wèn)題。實(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)然也可以用的做,不過(guò)用雙指針更快。 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
  • [Leetcode] Reverse Words in a String 反轉(zhuǎn)單詞順序

    摘要:代碼先反轉(zhuǎn)整個(gè)數(shù)組反轉(zhuǎn)每個(gè)單詞雙指針交換法復(fù)雜度時(shí)間空間思路這題就是版的做法了,先反轉(zhuǎn)整個(gè)數(shù)組,再對(duì)每個(gè)詞反轉(zhuǎn)。 Reverse Words in a String Given an input string, reverse the string word by word. For example, Given s = the sky is blue, return blue is...

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

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

0條評(píng)論

周?chē)?guó)輝

|高級(jí)講師

TA的文章

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