作者热门文章
- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
一、题目
二、示例
输入:nestedList = [[1,1],2,[1,1]]
输出:[1,1,2,1,1]
解释:通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,1,2,1,1]。
输入:nestedList = [1,[4,[6]]]
输出:[1,4,6]
解释:通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,4,6]。
三、思路
这里他给了一些方法,我们可以直接进行使用,比如说:isInteger()
,getInteger()
,getList()
。然后我们在代码中就可以使用这些方法了。比较类似于数组的扁平化。
四、代码展示
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* function NestedInteger() {
*
* Return true if this NestedInteger holds a single integer, rather than a nested list.
* @return {boolean}
* this.isInteger = function() {
* ...
* };
*
* Return the single integer that this NestedInteger holds, if it holds a single integer
* Return null if this NestedInteger holds a nested list
* @return {integer}
* this.getInteger = function() {
* ...
* };
*
* Return the nested list that this NestedInteger holds, if it holds a nested list
* Return null if this NestedInteger holds a single integer
* @return {NestedInteger[]}
* this.getList = function() {
* ...
* };
* };
*/
/**
* @constructor
* @param {NestedInteger[]} nestedList
*/
var NestedIterator = function (nestedList) {
this.vals = []
const rec = (nest) => {
for (let item of nest) {
if (item.isInteger()) {
this.vals.push(item.getInteger())
} else {
rec(item.getList())
}
}
}
rec(nestedList)
};
/**
* @this NestedIterator
* @returns {boolean}
*/
NestedIterator.prototype.hasNext = function () {
return this.vals.length > 0
};
/**
* @this NestedIterator
* @returns {integer}
*/
NestedIterator.prototype.next = function () {
return this.vals.shift()
};
/**
* Your NestedIterator will be called like this:
* var i = new NestedIterator(nestedList), a = [];
* while (i.hasNext()) a.push(i.next());
*/
五、总结
我有一个带有 UIPageViewController 的应用程序是这样设置的: UINavigationController *navController = (UINavigationContro
我有一个用于边框背景的 MVVM 绑定(bind),其中我从字符串格式的文件中读取颜色名称,并使用代码将它们转换为画笔: Brush b1 = new SolidColorBrush((Color)C
我有一个 D3 堆积面积图,我正在尝试重新计算刷牙时的 Y 轴。问题是,当我重新计算 Y 轴的最大值时,它不是所有不同层的组合,它只是最后一层。例如,如果我同时刷了 1/17 和 1/20 日期,我将
当程序上传到STM32F4时,有没有办法用计算机信息更新RTC?例如,电脑上的日期和小时信息是:12h40 11/09/2018,当我用IAR/AC6刷微 Controller 时,RTC是用这些信息
我是一名优秀的程序员,十分优秀!