Tags » Iterator

[LeetCode] Zigzag Iterator


2)用internal data structure自带的iterator


public class ZigzagIterator {
    private int x;
    private int y;
    private List<Integer> v1;
    private List<Integer> v2;
    private int cur;
    public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
        this.v1 = v1;
        this.v2 = v2;
        x = 0;
        y = 0;
        cur = 0;

    public int next() {
        int res;
        if (cur == 0) {
            if (x < v1.size()) {
                res = v1.get(x++);
            } else {
                res = v2.get(y++);
        } else {
            if (y < v2.size()) {
                res = v2.get(y++);
            } else {
                res = v1.get(x++);
        cur = 1 - cur;
        return res;

    public boolean hasNext() {
        if (v1 == null && v2 == null) {
            return false;
        } else if (v1 == null) {
            return y < v2.size();
        } else if (v2 == null) {
            return x < v1.size();
        } else {
            return x < v1.size() || y < v2.size();

 * Your ZigzagIterator object will be instantiated and called as such:
 * ZigzagIterator i = new ZigzagIterator(v1, v2);
 * while (i.hasNext()) v = i.next();
… 96 more words

Iterator Pattern

What is Iterator Design Pattern ?

  • is a behavioural design pattern
  • defines a way to move through a collection of data in a particular class…
  • 62 more words
Design Patterns

ZigZag Iterator

Problem: Given two 1d vectors, implement an iterator to return their elements alternately.

For example, given two 1d vectors:

v1 = 
v2 = 

By calling next repeatedly until hasNext returns… 101 more words


Spelunking Linux - what is this auxv thing anyway

While spelunking Linux, trying to find an easier way to do this or that, I ran across this vDSO thing (virtual ELF Dynamic Shared Object). What? 2,062 more words


Spelunking Linux - Yes, the system truly is a database

In this article: Isn’t the whole system just a database? – libdrm, I explored a little bit of the database nature of Linux by using libudev to enumerate and open libdrm devices.  842 more words


A skeleton code for Iterator in Rust

For record, I wrote down the same code.

Consuming Iterator

Iterator that does not consume items


Handling Multiple Windows in Selenium Webdriver

Hello friends. In this article we are going to see how to handle multiple windows in Selenium Webdriver. There are situations where we would have to deal with multiple windows. 265 more words