Java Solution Pre-process the input


  • 0

    My solution is pre-process the input when initialization.

    public class Vector2D implements Iterator<Integer> {
    
        List<Integer> list;
        Iterator<Integer> it;
        public Vector2D(List<List<Integer>> vec2d) {
            list = new ArrayList<Integer>();
            for (List<Integer> _list : vec2d) {
                for (Integer item : _list) {
                    list.add(item);
                }
            }
            
            it = list.iterator();
        }
    
        @Override
        public Integer next() {
            return it.next();
        }
    
        @Override
        public boolean hasNext() {
            return it.hasNext();
        }
    }
    

Log in to reply
 

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