Concise Java solution


  • -3
    Y

    Using one list to store the data.

    public class Vector2D {
    
    List<Integer> list = new ArrayList<Integer>();
    int ptr = 0;
    public Vector2D(List<List<Integer>> vec2d) {
        for(int i=0; i<vec2d.size(); i++){
            if(vec2d.get(i).size()!=0){
                list.addAll(vec2d.get(i));
            }
        }
    }
    
    public int next() {
        return list.get(ptr++);
    }
    
    public boolean hasNext() {
        return ptr<list.size();
    }
    

    }


Log in to reply
 

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