C# Using two Lists


  • 0
    S
    public class ZigzagIterator {
    
    public List<IList<int>> dict1{get;set;}
    public List<int> dict2{get;set;}
    public int curr{get;set;}
    public int total{get;set;}
    public int currarray{get;set;}
    
    public ZigzagIterator(IList<int> v1, IList<int> v2) {
        dict1=new List<IList<int>>{v1,v2};
        dict2=new List<int>{0,0};
        total=v1.Count+v2.Count;curr=0;currarray=0;
    }
    
    public bool HasNext() {
        return curr<total;
    }
    
    public int Next() {
        while(dict1[currarray].Count==dict2[currarray]){
            currarray=(currarray+1)%2;
        }
        int value=dict1[currarray][dict2[currarray]];
        dict2[currarray]++;
        currarray=(currarray+1)%2;
        curr++;
        return value;
    }
    

    }


Log in to reply
 

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