[栈] leetcode 341フラット化ネストされたリストイテレータ

問題:https://leetcode.com/problems/flatten-nested-list-iterator/

        この質問は、私は本当に痛いORZを書き込むための弱い鶏、手動でスタックを維持することです

クラスNestedIterator {
 パブリック
    ベクトル <NestedInteger> リスト。
    NestedIterator(ベクトル <NestedInteger>&nestedList){ 
        リスト = nestedList。
        もし(!nestedList.size()= 0 
            sta.push({ 0、&リスト})。
    } 
    スタック <ペア< INT、ベクトル<NestedInteger> * >> STA。
    int型データ;
    INT 次の(){
         戻りデータ; 
    } 
    
    ボイド予め()
    { 
        もし(sta.size()== 0のリターン; 
        sta.top()最初。 ++ ; 
        
        一方、(!。sta.empty()&& sta.top()第一> = sta.top()2次> サイズ())
        { 
            sta.pop()。
            もし(!sta.empty())sta.top()++最初の。; 
        }         
    } 
    
    BOOL のhasNext()
    { 
        場合(sta.size()== 0を返す 
        自動&CUR = * sta.top()は、第2。
        INT IDX = sta.top()最初。
        もし(curが[IDX] .isInteger()) 
        {
            データ = CUR [IDX] .getInteger()。
            前進(); 
            返す ; 
        } 
        
        { 
            場合(CUR [IDX] .getList()サイズ()> 0 
            { 
                sta.push({ 0、&cur変換[IDX] .getList()})。
                もし(のhasNext())は返す 
            } 
            事前()。
            返すのhasNextを();     
        }        
    } 
}。

 

おすすめ

転載: www.cnblogs.com/fish1996/p/11291579.html