short(9 lines),fast(beats 84.21%) and quite easy to understand


  • 0
    M

    Java

    public class Vector2D implements Iterator<Integer> {
    
        private Iterator<List<Integer>> all;//iterator for the vec2d
        private Iterator<Integer> cur;//iterator for the element in vec2d
    	
        public Vector2D(List<List<Integer>> vec2d) {
            all = vec2d.iterator();
        }
    
        @Override
        public Integer next() {
    	return cur.next();
        }
    
        @Override
        public boolean hasNext() {
    	if (cur != null && cur.hasNext()) return true;
    		
    	while(all.hasNext()){
    	    cur = all.next().iterator();
    	    if(cur.hasNext()) return true;//to handle [[],[]]
    	}
    	return false;
        }
    }
    
    

Log in to reply
 

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