查看: 2436| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

Flatten iterator 怎么做?

头像被屏蔽
提示: 作者被禁止或删除 内容自动屏蔽

上一篇:CC150有没有比较好的题解
下一篇:做题速度
🔗
austurela 2014-9-29 11:31:14 | 只看该作者
全局:
本帖最后由 austurela 于 2014-9-29 11:59 编辑

Please ignore...
回复

使用道具 举报

🔗
北美农民 2014-9-29 11:52:47 | 只看该作者
全局:
  1.   public static Iterator<String> flatten(final Iterator<Iterator<String>> iters) {
  2.       return new Iterator<String>() {
  3.         private Iterator<String> curIter = null;
  4.         private String nextItem = advanceItem();
  5.         
  6.         private String advanceItem() {
  7.           if (iters == null && curIter == null) throw new NullPointerException();
  8.             
  9.           while ((iters != null && iters.hasNext()) || (curIter != null && curIter.hasNext())) {
  10.             if ((curIter == null || !curIter.hasNext()))
  11.               if (iters != null && iters.hasNext())
  12.                 curIter = iters.next();

  13.             if (curIter == null) continue;
  14.             
  15.             while (curIter.hasNext()) {
  16.               String result = curIter.next();
  17.               if (result != null) return result;
  18.             }
  19.           }

  20.           return null;
  21.         }
  22.         
  23.         public boolean hasNext() {
  24.           return nextItem != null;
  25.         }

  26.         public String next() {
  27.           if (!hasNext())
  28.             throw new NullPointerException();

  29.           String oldItem = nextItem;
  30.           nextItem = advanceItem();
  31.           return oldItem;
  32.         }

  33.         public void remove() {
  34.           throw new UnsupportedOperationException();
  35.         }
  36.     };
  37.   }
复制代码

评分

参与人数 1大米 +5 收起 理由
hfzhangql + 5 原来是这样,iterator不熟,谢谢了~

查看全部评分

回复

使用道具 举报

🔗
lixiang.xjtu 2014-9-29 13:40:12 | 只看该作者
全局:
这个题我昨天中午做了。某公司的online 题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表