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

lc上的NestedInteger这个class到底如何定义呀

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
之前去onsite,因为是java选手,所以要写一个nestedinteger的class,(python应该是可以直接用了)。当时在面试官的指点下写出来了,现在想想又不会了。求指点!感谢!

可以看lc 341: https://leetcode.com/problems/fl ... erator/description/

上一篇:为什么很多DP问题的数组长度设置都是length+1
下一篇:求教Airbnb的boggle game
🔗
magicsets 2018-1-23 04:06:17 | 只看该作者
全局:
可以对NestedInteger接口实现两个子类,一个处理单个整数的情况,另一个处理List的情况:

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.Stack;

  5. public class Main {
  6.   public static void main(String[] args) {
  7.     // 测试数据
  8.     // [1, [2, 3], [[4, [], [5], 6, 7], 8], 9]
  9.     NestedInteger nestedList = new NestedList(
  10.         new SingletonInteger(1),
  11.         new NestedList(
  12.             new SingletonInteger(2),
  13.             new SingletonInteger(3)
  14.         ),
  15.         new NestedList(
  16.             new NestedList(
  17.                 new SingletonInteger(4),
  18.                 new NestedList(),
  19.                 new NestedList(
  20.                     new SingletonInteger(5)
  21.                 ),
  22.                 new SingletonInteger(6),
  23.                 new SingletonInteger(7)
  24.             ),
  25.             new SingletonInteger(8)
  26.         ),
  27.         new SingletonInteger(9)
  28.     );

  29.     NestedIterator it = new NestedIterator(nestedList.getList());
  30.     while (it.hasNext()) {
  31.       System.out.println(it.next());
  32.     }
  33.   }
  34. }


  35. interface NestedInteger {
  36.   public boolean isInteger();
  37.   public Integer getInteger();
  38.   public List<NestedInteger> getList();
  39. }

  40. // 处理单个整数
  41. class SingletonInteger implements NestedInteger {
  42.   private int value;

  43.   public SingletonInteger(int value) {
  44.     this.value = value;
  45.   }

  46.   @Override
  47.   public boolean isInteger() {
  48.     return true;
  49.   }

  50.   @Override
  51.   public Integer getInteger() {
  52.     return value;
  53.   }

  54.   @Override
  55.   public List<NestedInteger> getList() {
  56.     return null;
  57.   }
  58. }

  59. // 处理List
  60. class NestedList implements NestedInteger {
  61.   private List<NestedInteger> list = new ArrayList<NestedInteger>();

  62.   public NestedList(NestedInteger ...values) {
  63.     for (NestedInteger value : values) {
  64.       list.add(value);
  65.     }
  66.   }

  67.   @Override
  68.   public boolean isInteger() {
  69.     return false;
  70.   }

  71.   @Override
  72.   public Integer getInteger() {
  73.     return null;
  74.   }

  75.   @Override
  76.   public List<NestedInteger> getList() {
  77.     return list;
  78.   }
  79. }


  80. // LC 341
  81. class NestedIterator implements Iterator<Integer> {
  82.   private Stack<Iterator<NestedInteger>> state;
  83.   private boolean hasNextValue;
  84.   private Integer nextValue;

  85.   public NestedIterator(List<NestedInteger> nestedList) {
  86.     state = new Stack<Iterator<NestedInteger>>();
  87.     state.push(nestedList.iterator());
  88.     fetchNext();
  89.   }

  90.   @Override
  91.   public Integer next() {
  92.     Integer retValue = nextValue;
  93.     fetchNext();
  94.     return retValue;
  95.   }

  96.   @Override
  97.   public boolean hasNext() {
  98.     return hasNextValue;
  99.   }

  100.   private void fetchNext() {
  101.     while (!state.isEmpty()) {
  102.       Iterator<NestedInteger> it = state.pop();
  103.       if (it.hasNext()) {
  104.         NestedInteger ni = it.next();
  105.         state.push(it);
  106.         if (ni.isInteger()) {
  107.           hasNextValue = true;
  108.           nextValue = ni.getInteger();
  109.           return;
  110.         }
  111.         state.push(ni.getList().iterator());
  112.       }
  113.     }
  114.     hasNextValue = false;
  115.   }
  116. }
复制代码

评分

参与人数 1大米 +3 收起 理由
liu.haonan + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| liu.haonan 2018-3-10 12:57:34 | 只看该作者
全局:
magicsets 发表于 2018-1-23 04:06
可以对NestedInteger接口实现两个子类,一个处理单个整数的情况,另一个处理List的情况:

sorry,不明白为什么会miss到这个消息,现在才看到,先给层主热心答题点赞!非常感谢!
回复

使用道具 举报

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

本版积分规则

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