My simple and naive solution


  • 0
    W
    class ZigzagIterator {
    public:
        ZigzagIterator(vector<int>& v1, vector<int>& v2) {
            for (auto it1 = v1.begin(), it2 = v2.begin(); it1 != v1.end() || it2 != v2.end(); ){
                if (it1 != v1.end()){
                    combine.push_back(*it1);
                    it1++;
                }
                if (it2 != v2.end()){
                    combine.push_back(*it2);
                    it2++;
                }
            }
            index = 0;
        }
    
        int next() {
            return combine[index++];
        }
    
        bool hasNext() {
            return index < combine.size();
        }
    
    private:
        vector<int> combine;
        int index;
    
    };

Log in to reply
 

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