Clean C# based on Queue


  • 0
    R
    public class ZigzagIterator {
    
        private Queue<IList<int>> q;
        private Dictionary<IList<int>, int> dict;
        
        public ZigzagIterator(IList<int> v1, IList<int> v2) {
            q = new Queue<IList<int>>();
            // running index of each list
            dict = new Dictionary<IList<int>, int>();
            if(v1 != null && v1.Count != 0)
            {
                q.Enqueue(v1); dict[v1] = 0;
            }
            
            if(v2 != null && v2.Count != 0)
            {
                q.Enqueue(v2); dict[v2] = 0;
            }
        }
    
        public bool HasNext() {
            return q.Count != 0;
        }
    
        public int Next() {
            IList<int> l = q.Dequeue();
            int ret = l[dict[l]++];
            
            if(dict[l] < l.Count) q.Enqueue(l);
            
            return ret;
        }
    }
    
    /**
     * Your ZigzagIterator will be called like this:
     * ZigzagIterator i = new ZigzagIterator(v1, v2);
     * 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.