4ms solution beats 97.74% of java submissions


  • 0
    A
    public class Vector2D {
    
    List<Iterator<Integer>> itrs=new ArrayList();
    Iterator<Iterator<Integer>> listItr;
    Iterator<Integer> currentItr;
    public Vector2D(List<List<Integer>> vec2d) {
        for(List<Integer> list: vec2d){
            itrs.add(list.iterator());
        }
        listItr=itrs.iterator();
        if(listItr.hasNext())
            currentItr=listItr.next();
    }
    
    public int next() {
        return currentItr.next();
    }
    
    public boolean hasNext() {
        if(itrs.size()==0) return false;
        if(currentItr.hasNext()) return true;
        if(listItr.hasNext()){
            currentItr=listItr.next();
            return hasNext();
        }
        return false;
    }
    

    }


  • 0
    M

    with all due respect, copying the original data is not a good idea.


Log in to reply
 

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