284. Peeking Iterator

Question

Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be returned by the next call to next().

Here is an example. Assume that the iterator is initialized to the beginning of the list: [1, 2, 3].
Call next() gets you 1, the first element in the list.
Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.
You call next() the final time and it returns 3, the last element. Calling hasNext() after that should return false.


Code

// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
class PeekingIterator implements Iterator<Integer> {
    Integer remain;
    Iterator<Integer> i;

    public PeekingIterator(Iterator<Integer> iterator) {
        // initialize any member here.
        remain = null;
        i = iterator;
    }

    // Returns the next element in the iteration without advancing the iterator.
    public Integer peek() {
        if (remain == null) {
            remain = next();
        }
        return remain;
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public Integer next() {
        if (remain != null) {
            Integer ret = remain;
            remain = null;
            return ret;
        }
        return i.next();
    }

    @Override
    public boolean hasNext() {
        return remain != null || i.hasNext();
    }
}

Solution

用一個(gè)remain記錄移除的數(shù)字。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末懦冰,一起剝皮案震驚了整個(gè)濱河市廉羔,隨后出現(xiàn)的幾起案子总棵,更是在濱河造成了極大的恐慌哪怔,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件祖娘,死亡現(xiàn)場(chǎng)離奇詭異丽焊,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)眉孩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門个绍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人浪汪,你說(shuō)我怎么就攤上這事巴柿。” “怎么了死遭?”我有些...
    開(kāi)封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵广恢,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我呀潭,道長(zhǎng)钉迷,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任钠署,我火速辦了婚禮糠聪,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘谐鼎。我一直安慰自己舰蟆,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布狸棍。 她就那樣靜靜地躺著身害,像睡著了一般。 火紅的嫁衣襯著肌膚如雪隔缀。 梳的紋絲不亂的頭發(fā)上题造,一...
    開(kāi)封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音猾瘸,去河邊找鬼界赔。 笑死,一個(gè)胖子當(dāng)著我的面吹牛牵触,可吹牛的內(nèi)容都是我干的淮悼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼揽思,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼袜腥!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起钉汗,我...
    開(kāi)封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤羹令,失蹤者是張志新(化名)和其女友劉穎鲤屡,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體福侈,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡酒来,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了肪凛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片堰汉。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖伟墙,靈堂內(nèi)的尸體忽然破棺而出翘鸭,到底是詐尸還是另有隱情,我是刑警寧澤戳葵,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布就乓,位于F島的核電站,受9級(jí)特大地震影響譬淳,放射性物質(zhì)發(fā)生泄漏档址。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一邻梆、第九天 我趴在偏房一處隱蔽的房頂上張望守伸。 院中可真熱鬧,春花似錦浦妄、人聲如沸尼摹。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蠢涝。三九已至,卻和暖如春阅懦,著一層夾襖步出監(jiān)牢的瞬間和二,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工耳胎, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留惯吕,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓怕午,卻偏偏與公主長(zhǎng)得像废登,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子郁惜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容