成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[Leetcode] Peeking Iterator 瞥一眼迭代器

smallStone / 2476人閱讀

摘要:當(dāng)?shù)臅r(shí)候除了要返回緩存,還要將緩存更新為下一個(gè)數(shù)字,如果沒有下一個(gè)就將緩存更新為。代碼后續(xù)如何支持任意類型的迭代器使用的方式,代碼中已經(jīng)將替換為的

Peeking Iterator

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.

Hint:

Think of "looking ahead". You want to cache the next element. Is one variable sufficient? Why or why not? Test your design with call order of peek() before next() vs next() before peek().Show More Hint Follow up: How would you extend your design to be generic and work with all types, not just integer?

Credits: Special thanks to @porker2008 for adding this problem and creating all test cases.

緩存法 復(fù)雜度

時(shí)間 O(1) 空間 O(1)

思路

為了能peek后下次next還得到同樣的數(shù)字,我們要用一個(gè)緩存保存下一個(gè)數(shù)字。這樣當(dāng)peek時(shí)候,返回緩存就行了,迭代器位置也不會(huì)變。當(dāng)next的時(shí)候除了要返回緩存,還要將緩存更新為下一個(gè)數(shù)字,如果沒有下一個(gè)就將緩存更新為null。

代碼
class PeekingIterator implements Iterator {

    T cache;
    Iterator it;

    public PeekingIterator(Iterator iterator) {
        // initialize any member here.
        this.cache = iterator.next();
        this.it = iterator;
    }

    // Returns the next element in the iteration without advancing the iterator.
    public T peek() {
        return cache;
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public T next() {
        T res = cache;
        cache = it.hasNext() ? it.next() : null;
        return res;
    }

    @Override
    public boolean hasNext() {
        return it.hasNext() || cache != null;
    }
}
后續(xù) Follow Up

Q:如何支持任意類型的迭代器?
A:使用Generic的方式,代碼中已經(jīng)將Integer替換為Generic的T

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/64577.html

相關(guān)文章

  • 【LC總結(jié)】Iterator題目<Zigzag 1&2><BST>&

    摘要:方法直接查找數(shù)組的位的迭代器,調(diào)用方法得到的整數(shù)即為要返回的元素。再寫迭代器的方法返回指針元素的并讓指針通過遞歸方法指向下一個(gè)元素。 Zigzag Iterator Problem Given two 1d vectors, implement an iterator to return their elements alternately. Example Given two 1d ...

    WelliJhon 評(píng)論0 收藏0
  • leetcode 341 Flatten Nested List Iterator 以及其他Iter

    摘要:返回的是表示是否走到了結(jié)尾。起到的就是緩存作用,因?yàn)檎{(diào)用之后馬上就走到下一個(gè)了。如果調(diào)用,返回用得到和最初的輸入相同的做相同的步驟存入不斷拆開得到結(jié)果。思想就是來(lái)自括號(hào),后面也會(huì)跟進(jìn)的專題 Iterator其實(shí)就是一個(gè)單鏈表,無(wú)法回頭看。java里很多數(shù)據(jù)結(jié)構(gòu)都有這個(gè)接口,使用時(shí)需要initalize,得到一個(gè)iterator. 調(diào)用next()返回的是一個(gè)object, 指向的是下一...

    chaosx110 評(píng)論0 收藏0
  • [Leetcode] Zigzag Iterator Z形迭代

    摘要:用變量和取模來(lái)判斷我們?cè)撊×斜碇械牡趲讉€(gè)迭代器。同樣,由于每用完一個(gè)迭代器后都要移出一個(gè),變量也要相應(yīng)的更新為該迭代器下標(biāo)的上一個(gè)下標(biāo)。如果迭代器列表為空,說明沒有下一個(gè)了。 Zigzag Iterator Given two 1d vectors, implement an iterator to return their elements alternately. For exa...

    SolomonXie 評(píng)論0 收藏0
  • [Leetcode] Flatten 2D Vector 整平二維向量

    摘要:另一個(gè)則是的迭代器,它負(fù)責(zé)記錄當(dāng)前到哪一個(gè)的迭代器了。每次時(shí),我們先調(diào)用一下,確保當(dāng)前的迭代器有下一個(gè)值。代碼當(dāng)前列表的迭代器為空,或者當(dāng)前迭代器中沒有下一個(gè)值時(shí),需要更新為下一個(gè)迭代器 Flatten 2D Vector Implement an iterator to flatten a 2d vector. For example, Given 2d vector = [ ...

    MageekChiu 評(píng)論0 收藏0
  • [Leetcode] Binary Search Tree Iterator 二叉搜素樹迭代

    摘要:代碼先找到第一個(gè)節(jié)點(diǎn),并把路徑入棧棧為空時(shí)不再有下一個(gè)棧頂是待返回元素如果該元素有右節(jié)點(diǎn),把它的右節(jié)點(diǎn)及右節(jié)點(diǎn)的所有左邊節(jié)點(diǎn)都?jí)喝霔V? Binary Search Tree Iterator 最新更新:https://yanjia.me/zh/2019/02/... Implement an iterator over a binary search tree (BST). Your...

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

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

0條評(píng)論

閱讀需要支付1元查看
<