<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)281:鋸齒迭代器

          共 3104字,需瀏覽 7分鐘

           ·

          2021-06-02 20:38

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

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

          Given two 1d vectors, implement an iterator to return their elements alternately.

          給出兩個一維的向量,請你實現(xiàn)一個迭代器,交替返回它們中間的元素。

          示例


          輸入:
          v1 = [1,2]
          v2 = [3,4,5,6]
          輸出: [1,3,2,4,5,6]

          解析: 通過連續(xù)調用 next 函數(shù)直到 hasNext 函數(shù)返回 false
               next 函數(shù)返回值的次序應依次為: [1,3,2,4,5,6]。
               
          拓展:假如給你 k 個一維向量呢?你的代碼在這種情況下的擴展性又會如何呢?


          解題

          https://blog.csdn.net/qq_29051413/article/details/108616790


          public class ZigzagIterator {
              List<List<Integer>> data = new ArrayList<>(); //所有數(shù)據(jù)
              int size; //數(shù)據(jù)列表的總數(shù)
              int cur; //當前遍歷到的list的序號
              List<Integer> index = new ArrayList(); //每個list中當前遍歷到的位置index

              public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
                  data.add(v1);
                  data.add(v2);
                  size = 2; //目前只有兩個列表,可擴展為k個
                  cur = 0; //從第0個list開始遍歷
                  index.add(0);
                  index.add(0);
              }

              public int next() {
                  int ans = data.get(cur).get(index.get(cur)); //取出當前的數(shù)據(jù)
                  index.set(cur,index.get(cur)+1); //更改位置到下一個數(shù)據(jù),有效性有hasNext來保證
                  cur = (cur+1) % size;
                  return ans;
              }

              public boolean hasNext() {
                  int start = cur; //保存目前的序號,用來判斷是否所有l(wèi)ist都已經遍歷到了最后一個位置
                  while(index.get(cur)==data.get(cur).size()){
                      cur = (cur+1) % size;

                      /**
                      運行到下一個if語句,說明所有的list都在最后一個位置,cur回到了最開始進入while循環(huán)的位置
                      此時,所有數(shù)據(jù)都已經遍歷完成,返回false
                      如果沒有這一句,會進入無限循環(huán)
                      */

                      if(cur == start) return false;
                  }
                  return true;
              }
          }


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

          上期推文:

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


          瀏覽 107
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  久久免费精品一区二区三区 | 国产精品视频你懂的 | 韩国一区二 | 综合一区二区三区 | 欧美性xxx88 |