251. Flatten 2D Vector


Implement an iterator to flatten a 2d vector.

For example,
Given 2d vector =

[
  [1,2],
  [3],
  [4,5,6]
]

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,2,3,4,5,6].

刚开始想到的写法是:

class Vector2D {
private:
    vector<vector<int>>::iterator start, end;
    int j = 0;
public:
    Vector2D(vector<vector<int>>& vec2d) {
        start = vec2d.begin();
        end = vec2d.end();
    }

    int next() {
        int res = (*start)[j];
        if (j >= (*start).size() - 1 && start != end) {//remember to check whether start = end,or start will be end+1
            start++;
            j = 0;//remember to reset j to 0
        }
        else j++;
        return res;
    }

    bool hasNext() {
        if (start == end && j == (*start).size()) return false;
        else return true;
    }
};

/**
 * Your Vector2D object will be instantiated and called as such:
 * Vector2D i(vec2d);
 * while (i.hasNext()) cout << i.next();
 */

用hasNext 判断是否到达到达末端,用next做所有的下标更新和返回值。发现这样不能处理空vector的情况。然后做了一系列的边界处理,发现必须在hasNext判断是否当前为空vector,不然就要在next返回值,而空串没有值,所以会出错。

注意 end()是vector最后一个元素的下一个iterator,不指向任何值。

class Vector2D {
private:
    vector<vector<int>>::iterator start, end;
    int j = 0;
public:
    Vector2D(vector<vector<int>>& vec2d) {
        start = vec2d.begin();
        end = vec2d.end();        
    }

    int next() {
        hasNext();
        return (*start)[j++];
    }

    bool hasNext() {
        while(start != end && j == (*start).size()) {
            start++;
            j = 0;
        }
        return start != end;//include empty vector and the end of the vector
    }
};

/**
 * Your Vector2D object will be instantiated and called as such:
 * Vector2D i(vec2d);
 * while (i.hasNext()) cout << i.next();
 */

在hasNext更新所指行,在next更新此行所指int,注意hasNext中return start!=end 包含了两种情况:

1 return false :

a. 当前所指vector为空,则start == end 判断空vector 很重要

b. 当前到了整个vector的末尾,则start == end

2 return true

start != end

加上remove的版本:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <list>
using namespace std;

class Vector2D {
private:
    vector<vector<int>>::iterator start, end;
    int j = 0;
public:
    Vector2D(vector<vector<int>>& vec2d) {
        start = vec2d.begin();
        end = vec2d.end();
    }
    
    int next() {
        //hasNext();
        return (*start)[j];
    }
    
    bool hasNext() {
        while(start != end && j >= (*start).size()) {
            start++;
            j = 0;
        }
        return start != end;//include empty vector and the end of the vector
    }
    
    void remove() {
        vector<int>::iterator it;
        it = (*start).begin();
        (*start).erase(it+j);
    }
};

//int main() {
//    vector<vector<int>> matrix;
//    
//    vector<int> row1;
//    row1.push_back(1);
//    row1.push_back(2);
//    matrix.push_back(row1);
//    vector<int> row2;
//    row2.push_back(3);
//    matrix.push_back(row2);
//    vector<int> row3;
//    matrix.push_back(row3);
//    vector<int> row4;
//    row4.push_back(4);
//    row4.push_back(5);
//    row4.push_back(6);
//    matrix.push_back(row4);
//    
//    Vector2D i(matrix);
//    while(i.hasNext()) {
//        cout<<i.next()<<endl;
//        i.remove();
//    }
//    
//    while(i.hasNext()) {
//        cout<<i.next()<<endl;
//    }
//    return 0;
//}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值