LeetCode Top Interview Questions 251. Flatten 2D Vector (Java版; Medium)

welcome to my blog

LeetCode Top Interview Questions 251. Flatten 2D Vector (Java版; Medium)

题目描述
Design and implement an iterator to flatten a 2d vector. It should support the following operations: next and hasNext.

 

Example:

Vector2D iterator = new Vector2D([[1,2],[3],[4]]);

iterator.next(); // return 1
iterator.next(); // return 2
iterator.next(); // return 3
iterator.hasNext(); // return true
iterator.hasNext(); // return true
iterator.next(); // return 4
iterator.hasNext(); // return false
 

Notes:

Please remember to RESET your class variables declared in Vector2D, as static/class variables are persisted
across multiple test cases. Please see here for more details.
You may assume that next() call will always be valid, that is, there will be at least a next element in the
2d vector when next() is called.
 

Follow up:

As an added challenge, try to code it using only iterators in C++ or iterators in Java.
第一次做; 维护两个指针, 先跳过空的一位数组
//由于是二维向量, 所以维护两个指针即可
class Vector2D {
    private int i;
    private int j;
    private int[][] v;
    
    
    public Vector2D(int[][] v) {
        this.v = v;
        skipNull();
    }
    
    public int next() {
        int res = v[i][j];
        //update i, j
        //超细节: 先增加j, 再检查j是否越界!!!!!!!
        j++;
        if(j==v[i].length){
            i++;
            //检查v[i]长是否为0, 是的话i往后跳,直到v[i]长度不为0
            skipNull();
            j=0;
        }
        return res;
    }
    
    public boolean hasNext() {
        return i<v.length && j<v[i].length;
    }
    
    private void skipNull(){
        while(i<v.length && v[i].length==0)
            i++;
    }
}
第一次做; 接收二维数组后直接将所有数展开, 使用了递归(不用递归也行, 因为题目中shuole,这是个二维向量, 不存在循环嵌套的情况); 栈中存储的是Integer
/*
问问面试官, 输入中是否有[]
*/
class Vector2D {
    private Stack<Integer> stack;
    public Vector2D(int[][] v) {
        stack = new Stack<>();
        flatten(v, v.length-1);
    }
    
    public int next() {
        return stack.pop();
    }
    
    public boolean hasNext() {
        return !stack.isEmpty();
    }
    
    private void flatten(int[][] arr, int index){
        for(int i=index; i>=0; i--){
            if(arr[i].length==1)
                stack.push(arr[i][0]);
            else{
                int[][] tmp = new int[arr[i].length][1];
                for(int j=0; j<tmp.length; j++)
                    tmp[j][0] = arr[i][j];
                flatten(tmp, tmp.length-1);
            }  
        }
    }
}

/**
 * Your Vector2D object will be instantiated and called as such:
 * Vector2D obj = new Vector2D(v);
 * int param_1 = obj.next();
 * boolean param_2 = obj.hasNext();
 */
力扣优秀题解
class Vector2D {

    int i = 0;
    int j = 0;
    int[][] v;
    public Vector2D(int[][] v) {
        this.v = v;
        skipEmpty();
    }
    
    public int next() {
        int result = v[i][j];
        j ++;
        if (v[i].length == j) {
            i ++;
            skipEmpty();
            j = 0;
        }
        return result;
    }
    
    public void skipEmpty() {
        // skip empty.
        while (i < v.length && v[i].length == 0) {
            i ++;
        }
    }

    public boolean hasNext() {
        return i < v.length && j < v[i].length;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值