首页 > 其他 > 详细

LeetCode "Zigzag Iterator"

时间:2015-09-15 06:54:52      阅读:238      评论:0      收藏:0      [点我收藏+]

Capable to k-vector input too:

class ZigzagIterator {
    int x;    
    int i;
    int max_x;
    vector<vector<int>*> l;
    
    void moveon()
    {
        int oldi= i;
        i = (i + 1) % l.size();
        x += i <= oldi;
    }
public:
    ZigzagIterator(vector<int>& v1, vector<int>& v2) 
    {
        i = x = 0;
        max_x = max(v1.size(), v2.size());

        if(v1.size() > 0)    l.push_back(&v1);
        if(v2.size() > 0)    l.push_back(&v2);
    }

    int next() 
    {
        int ret = (*l[i])[x];
        while(moveon(), x < max_x && l[i]->size() <= x);return ret;
     return ret; }
bool hasNext() { return x < max_x; } };

LeetCode "Zigzag Iterator"

原文:http://www.cnblogs.com/tonix/p/4809038.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!