C++ O(min(n,m)) solution, have fun


  • 1
    class ZigzagIterator {
        int cur;
        vector<int> res;
    public:
        ZigzagIterator(vector<int>& v1, vector<int>& v2) {
            cur=0;
            if(v1.size()>v2.size())
                helper(v1,v2,true);
            else
                helper(v2, v1,false);
        }
        void helper(vector<int>& v1, vector<int> v2, bool flag)
        {
            res =v1;
            int step = 0;
            for(int i=0; i<v2.size(); i++)
            {
                step = i*2+1;
                res.insert(res.begin()+step, v2[i]);
                if(!flag)
                    swap(res[step], res[step-1]);
            }
        }
        int next() {
            return res[cur++];
        }
    
        bool hasNext() {
            return cur<res.size();
        }
    };

Log in to reply
 

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