java 86% easy to understand


  • 0
    G
    public class Vector2D implements Iterator<Integer> {
        List<List<Integer>> vec2d;
        List<Integer> as = new ArrayList<Integer>();
        int index=0;
        
        public Vector2D(List<List<Integer>> vec2d) {
            this.vec2d = vec2d;
            for(List<Integer> tp:vec2d){
                if(!tp.isEmpty()){
                    as.addAll(tp);
                }
            }
        }
    
        @Override
        public Integer next() {
            index++;
            return as.get(index-1);
        }
    
        @Override
        public boolean hasNext() {
            if(index>=as.size()){
                return false;
            }
            return true;
        }
    }
    
    /**
     * Your Vector2D object will be instantiated and called as such:
     * Vector2D i = new Vector2D(vec2d);
     * 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.