<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)284:頂端迭代器

          共 3610字,需瀏覽 8分鐘

           ·

          2021-06-08 02:17

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 頂端迭代器,我們先來看題面:
          https://leetcode-cn.com/problems/peeking-iterator/

          Design an iterator that supports the peek operation on a list in addition to the hasNext and the next operations.
          Implement the PeekingIterator class:
          • PeekingIterator(int[] nums) Initializes the object with the given integer array nums.

          • int next() Returns the next element in the array and moves the pointer to the next element.

          • bool hasNext() Returns true if there are still elements in the array.

          • int peek() Returns the next element in the array without moving the pointer.






          給定一個迭代器類的接口,接口包含兩個方法:next() 和 hasNext()。設(shè)計并實現(xiàn)一個支持 peek() 操作的頂端迭代器 -- 其本質(zhì)就是把原本應(yīng)由 next() 方法返回的元素 peek() 出來。

          示例


          假設(shè)迭代器被初始化為列表 [1,2,3]。

          調(diào)用 next() 返回 1,得到列表中的第一個元素。
          現(xiàn)在調(diào)用 peek() 返回 2,下一個元素。在此之后調(diào)用 next() 仍然返回 2。
          最后一次調(diào)用 next() 返回 3,末尾元素。在此之后調(diào)用 hasNext() 應(yīng)該返回 false

          進(jìn)階:你將如何拓展你的設(shè)計?使之變得通用化,從而適應(yīng)所有的類型,而不只是整數(shù)型?


          解題


          設(shè)一個 peeked 的flag 同時 保存 peeked 過得值.  如果已經(jīng)peeked過了, next()直接返回保存的值即可.

          // Java Iterator interface reference:
           // https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
           class PeekingIterator implements Iterator<Integer> {
               Iterator<Integer> itr;
               boolean peeked;
               int peekVal;
           
               public PeekingIterator(Iterator<Integer> iterator) {
                   // initialize any member here.
                   this.itr = iterator;
               }
           
               // Returns the next element in the iteration without advancing the iterator.
               public Integer peek() {
                   if(peeked) {
                       return peekVal;
                   } else {
                       peeked = true;
                       peekVal = itr.next();
                   }
                   return peekVal;
               }
           
               // hasNext() and next() should behave the same as in the Iterator interface.
               // Override them if needed.
               @Override
               public Integer next() {
                   if(peeked) {
                       peeked = false;
                       return peekVal;
                   } else return itr.next();
               }
           
               @Override
               public boolean hasNext() {
                   return peeked || itr.hasNext();
               }
           }


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-280題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)281:鋸齒迭代器

          LeetCode刷題實戰(zhàn)282:給表達(dá)式添加運算符

          LeetCode刷題實戰(zhàn)283:移動零


          瀏覽 19
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  又黄又爽的网站 | 特色特黄网址 | 无码日韩人妻精品久久蜜桃 | 国产精品国产三级国产 | 狠狠操人人摸 |