博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flatten Nested List Iterator
阅读量:6007 次
发布时间:2019-06-20

本文共 1858 字,大约阅读时间需要 6 分钟。

1 /** 2  * // This is the interface that allows for creating nested lists. 3  * // You should not implement it, or speculate about its implementation 4  * public interface NestedInteger { 5  * 6  *     // @return true if this NestedInteger holds a single integer, rather than a nested list. 7  *     public boolean isInteger(); 8  * 9  *     // @return the single integer that this NestedInteger holds, if it holds a single integer10  *     // Return null if this NestedInteger holds a nested list11  *     public Integer getInteger();12  *13  *     // @return the nested list that this NestedInteger holds, if it holds a nested list14  *     // Return null if this NestedInteger holds a single integer15  *     public List
getList();16 * }17 */18 public class NestedIterator implements Iterator
{19 private Deque
flatten;20 public NestedIterator(List
nestedList) {21 flatten = new LinkedList<>();22 getFlatten(nestedList);23 }24 25 private void getFlatten(List
nestedList) {26 for (int i = 0; i < nestedList.size(); i++) {27 if (nestedList.get(i).isInteger()) {28 flatten.offer(nestedList.get(i).getInteger());29 } else {30 getFlatten(nestedList.get(i).getList());31 }32 }33 }34 35 @Override36 public Integer next() {37 return flatten.pollFirst();38 }39 40 @Override41 public boolean hasNext() {42 return flatten.size() > 0;43 }44 }45 46 /**47 * Your NestedIterator object will be instantiated and called as such:48 * NestedIterator i = new NestedIterator(nestedList);49 * while (i.hasNext()) v[f()] = i.next();50 */

 

转载于:https://www.cnblogs.com/shuashuashua/p/5631739.html

你可能感兴趣的文章
.net中验证码的几种常用方法
查看>>
解决OracleDBConsoleorcl不能启动
查看>>
.net DLL程序集中打包另一个DLL
查看>>
我的友情链接
查看>>
Drupal第三方模块汇集(一)
查看>>
我的友情链接
查看>>
使用spring的自身的listener进行web的配置
查看>>
linux学习之“VI”与“VIM”
查看>>
linux下无线网卡驱动安装
查看>>
oracle recyclebin与flashback drop
查看>>
我的友情链接
查看>>
svmlight使用说明
查看>>
LVM
查看>>
学习之shell脚本
查看>>
Andorid Launcher程序代码分析
查看>>
Swing 和AWT之间的关系
查看>>
Mysql设置自增长主键的初始值
查看>>
Android计时器正确应用方式解析
查看>>
性能及监控
查看>>
linux系统CPU、内存、硬盘、网络、lnmp服务整体监控邮件报警
查看>>