C# two solutions using List and stack


  • 0
    Y

    //using stack

    /**
     * // This is the interface that allows for creating nested lists.
     * // You should not implement it, or speculate about its implementation
     * interface NestedInteger {
     *
     *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
     *     bool IsInteger();
     *
     *     // @return the single integer that this NestedInteger holds, if it holds a single integer
     *     // Return null if this NestedInteger holds a nested list
     *     int GetInteger();
     *
     *     // @return the nested list that this NestedInteger holds, if it holds a nested list
     *     // Return null if this NestedInteger holds a single integer
     *     IList<NestedInteger> GetList();
     * }
     */
    public class NestedIterator {
        //stack 
        Stack<NestedInteger> stack=new Stack<NestedInteger>();
        public NestedIterator(IList<NestedInteger> nestedList) {
            breakList(nestedList);
        }
        public bool HasNext() {
            if(stack.Count==0)
                return false;
            if(stack.Peek().IsInteger())
                {return true;}
            else
            {
                breakList(stack.Pop().GetList());
                return HasNext();
            }
        }
        private void breakList(IList<NestedInteger> nestedList)
        {
            for(int i=nestedList.Count-1;i>=0;i--)
                stack.Push(nestedList[i]);
        }
        public int Next() {
            if(stack.Peek().IsInteger())
                {return stack.Pop().GetInteger();}
            else
            {
                breakList(stack.Pop().GetList());
                return Next();
            }
        }
    }
    
    /**
     * Your NestedIterator will be called like this:
     * NestedIterator i = new NestedIterator(nestedList);
     * while (i.HasNext()) v[f()] = i.Next();
     */
    
       //using list   
    /**
     * // This is the interface that allows for creating nested lists.
     * // You should not implement it, or speculate about its implementation
     * interface NestedInteger {
     *
     *     // @return true if this NestedInteger holds a single integer, rather than a nested list.
     *     bool IsInteger();
     *
     *     // @return the single integer that this NestedInteger holds, if it holds a single integer
     *     // Return null if this NestedInteger holds a nested list
     *     int GetInteger();
     *
     *     // @return the nested list that this NestedInteger holds, if it holds a nested list
     *     // Return null if this NestedInteger holds a single integer
     *     IList<NestedInteger> GetList();
     * }
     */
    public class NestedIterator {
        List<NestedInteger> list=new List<NestedInteger>();
        public NestedIterator(IList<NestedInteger> nestedList) {
            list=nestedList.ToList();
        }
        public bool HasNext() {
            if(list.Count==0)
                return false;
            if(list[0].IsInteger())
                {return true;}
            else
            {
                breakList();
                return HasNext();
            }
        }
        private void breakList()
        {
            var newList=new List<NestedInteger>();
            newList.AddRange(list[0].GetList());
            list.RemoveAt(0);
            newList.AddRange(list);
            list=newList;
        }
        public int Next() {
            if(list[0].IsInteger())
            {
                int res=list[0].GetInteger();
                list.RemoveAt(0);
                return res;
            }
            else
            {
                breakList();
                return Next();
            }
        }
    }
    
    /**
     * Your NestedIterator will be called like this:
     * NestedIterator i = new NestedIterator(nestedList);
     * while (i.HasNext()) v[f()] = i.Next();
     */

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.