我在 Leetcode 中做这个问题。原代码是:/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * public interface NestedInteger { * * // @return true if this NestedInteger holds a single integer, rather than a nested list. * public boolean isInteger(); * * // @return the single integer that this NestedInteger holds, if it holds a single integer * // Return null if this NestedInteger holds a nested list * public Integer getInteger(); * * // @return the nested list that this NestedInteger holds, if it holds a nested list * // Return null if this NestedInteger holds a single integer * public List<NestedInteger> getList(); * } */public class NestedIterator implements Iterator<Integer> { public NestedIterator(List<NestedInteger> nestedList) { } @Override public Integer next() { } @Override public boolean hasNext() { }}/** * Your NestedIterator object will be instantiated and called as such: * NestedIterator i = new NestedIterator(nestedList); * while (i.hasNext()) v[f()] = i.next(); */我意识到NestedIterator构造函数使用接口列表作为参数。我很困惑为什么它没有在某处实现接口,而是直接在列表中使用它。我认为我们不应该直接使用接口。谢谢!
2 回答
开心每一天1111
TA贡献1836条经验 获得超13个赞
根据任务描述 You should not implement it (NestedInteger), or speculate about its implementation
在现实世界中,您必须在使用之前声明接口并在某处实现它。然而,在这个简化的任务中,您只需要实现NestedIterator
类。LeetCode 负责NestedInteger
以正确的方式实现 - 您只需要NestedInteger
仔细阅读实现说明并在代码中使用它的方法。
我感到困惑为什么它没有在某处实现接口
实际上确实如此。在编译 LeetCode 时,它会修改您的代码(例如通过添加import some.package.NestedInteger
),使其不会引发编译错误。
添加回答
举报
0/150
提交
取消