L家的高频题merge k sorted arrays giving iterators求讨论!# JobHunting - 待字闺中
y*e
1 楼
interface是这个 Iterable mergeKSortedIterators(Iterators[] iters)
也就是给每个array的iterator,然后merge.
我开始想的是用merge k sorted list的办法,用一个heap保存所有的iterator,看哪个
iterator的头个element最小,push to result,然后Move to next element。
需要一个iterator的comparator来sort iterators, 是我的comparator写的不对?还
是heap的operation错了?
不知有人做过这题吗,希望给点建议。。。。
public static class iteratorComp implements Comparator>{
public int compare(Iterator a, Iterator b){
int a1 = 0;
int b1 = 0;
if(a.hasNext()){
a1 = a.next();
}
if(b.hasNext()){
b1 = b.next();
}
return a1 - b1;
}
}
也就是给每个array的iterator,然后merge.
我开始想的是用merge k sorted list的办法,用一个heap保存所有的iterator,看哪个
iterator的头个element最小,push to result,然后Move to next element。
需要一个iterator的comparator来sort iterators, 是我的comparator写的不对?还
是heap的operation错了?
不知有人做过这题吗,希望给点建议。。。。
public static class iteratorComp implements Comparator
public int compare(Iterator
int a1 = 0;
int b1 = 0;
if(a.hasNext()){
a1 = a.next();
}
if(b.hasNext()){
b1 = b.next();
}
return a1 - b1;
}
}