Created
April 25, 2017 03:01
-
-
Save xcv58/7667b56cdfe0178b029147464cc64390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyIter implements Iterator { | |
private Iter a; | |
private Iter b; | |
private Integer common; | |
public MyIter(Iter a, Iter b) { | |
if (a == null || b == null) { | |
throw new Exceptin("Null"); | |
} | |
this.a = a; | |
this.b = b; | |
this.common = null; | |
} | |
public Integer next() { | |
if (!hasNext()) { | |
throw new Exceptin(); | |
} | |
Integer res = this.common; | |
this.common = null; | |
return res; | |
} | |
public boolean hasNext() { | |
if (this.common != null) { | |
return true; | |
} | |
Integer aa = this.getNext(this.a); | |
Integer bb = this.getNext(this.b); | |
if (aa == null || bb == null) { | |
return false; | |
} | |
while (!aa.equals(bb)) { | |
if (aa < bb) { | |
aa = this.getNext(this.a); | |
} else { | |
bb = this.getNext(this.b); | |
} | |
if (aa == null || bb == null) { | |
return false; | |
} | |
} | |
this.common = aa; | |
return true; | |
} | |
private Integer getNext(Iterator e) { | |
if (e.hasNext()) { | |
return e.next(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment