扁平化嵌套列表迭代器
描述
-
给你一个嵌套的整数列表 nestedList 。每个元素要么是一个整数,要么是一个列表;该列表的元素也可能是整数或者是其他列表。请你实现一个迭代器将其扁平化,使之能够遍历这个列表中的所有整数。
-
实现扁平迭代器类 NestedIterator :
NestedIterator(List<NestedInteger> nestedList)用嵌套列表 nestedList 初始化迭代器int next()返回嵌套列表的下一个整数boolean hasNext()如果仍然存在待迭代的整数,返回 true ;否则,返回 false
-
你的代码将会用下述伪代码检测:
initialize iterator with nestedList res = [] while iterator.hasNext() append iterator.next() to the end of res return res -
如果 res 与预期的扁平化列表匹配,那么你的代码将会被判为正确
示例 1
输入:nestedList = [[1,1],2,[1,1]]
输出:[1,1,2,1,1]
解释:通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,1,2,1,1]
示例 2
输入:nestedList = [1,[4,[6]]]
输出:[1,4,6]
解释:通过重复调用 next 直到 hasNext 返回 false,next 返回的元素的顺序应该是: [1,4,6]
提示
- 1 <= nestedList.length <= 500
- 嵌套列表中的整数值在范围 [- 1 0 6 10^6 106, 1 0 6 10^6 106] 内
Typescript 版算法实现
1 ) 方案1:深度优先搜索
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* If value is provided, then it holds a single integer
* Otherwise it holds an empty nested list
* constructor(value?: number) {
* ...
* };
*
* Return true if this NestedInteger holds a single integer, rather than a nested list.
* isInteger(): boolean {
* ...
* };
*
* Return the single integer that this NestedInteger holds, if it holds a single integer
* Return null if this NestedInteger holds a nested list
* getInteger(): number | null {
* ...
* };
*
* Set this NestedInteger to hold a single integer equal to value.
* setInteger(value: number) {
* ...
* };
*
* Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
* add(elem: NestedInteger) {
* ...
* };
*
* Return the nested list that this NestedInteger holds,
* or an empty list if this NestedInteger holds a single integer
* getList(): NestedInteger[] {
* ...
* };
* };
*/
class NestedIterator {
vals = []
constructor(nestedList: NestedInteger[]) {
this.vals = [];
const dfs = (nestedList) => {
for (const nest of nestedList) {
if (nest.isInteger()) {
this.vals.push(nest.getInteger());
} else {
dfs(nest.getList());
}
}
}
dfs(nestedList);
}
hasNext(): boolean {
return this.vals.length > 0;
}
next(): number {
const val = this.vals[0];
this.vals = this.vals.slice(1);
return val;
}
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* var obj = new NestedIterator(nestedList)
* var a: number[] = []
* while (obj.hasNext()) a.push(obj.next());
*/
2 ) 方案2:栈
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* If value is provided, then it holds a single integer
* Otherwise it holds an empty nested list
* constructor(value?: number) {
* ...
* };
*
* Return true if this NestedInteger holds a single integer, rather than a nested list.
* isInteger(): boolean {
* ...
* };
*
* Return the single integer that this NestedInteger holds, if it holds a single integer
* Return null if this NestedInteger holds a nested list
* getInteger(): number | null {
* ...
* };
*
* Set this NestedInteger to hold a single integer equal to value.
* setInteger(value: number) {
* ...
* };
*
* Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
* add(elem: NestedInteger) {
* ...
* };
*
* Return the nested list that this NestedInteger holds,
* or an empty list if this NestedInteger holds a single integer
* getList(): NestedInteger[] {
* ...
* };
* };
*/
class NestedIterator {
stack = []
constructor(nestedList: NestedInteger[]) {
this.stack = nestedList;
}
hasNext(): boolean {
while (this.stack.length !== 0) {
if (this.stack[0].isInteger()) {
return true;
} else {
let cur = this.stack[0].getList();
this.stack.shift();
this.stack.unshift(...cur);
}
}
}
next(): number {
return this.stack.shift().getInteger();
}
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* var obj = new NestedIterator(nestedList)
* var a: number[] = []
* while (obj.hasNext()) a.push(obj.next());
*/
3 ) 方案3:另一种栈
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* If value is provided, then it holds a single integer
* Otherwise it holds an empty nested list
* constructor(value?: number) {
* ...
* };
*
* Return true if this NestedInteger holds a single integer, rather than a nested list.
* isInteger(): boolean {
* ...
* };
*
* Return the single integer that this NestedInteger holds, if it holds a single integer
* Return null if this NestedInteger holds a nested list
* getInteger(): number | null {
* ...
* };
*
* Set this NestedInteger to hold a single integer equal to value.
* setInteger(value: number) {
* ...
* };
*
* Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
* add(elem: NestedInteger) {
* ...
* };
*
* Return the nested list that this NestedInteger holds,
* or an empty list if this NestedInteger holds a single integer
* getList(): NestedInteger[] {
* ...
* };
* };
*/
// 假设 NestedInteger 接口已经定义好,这里提供一个简化版本
interface NestedInteger {
isInteger(): boolean;
getInteger(): number;
getList(): NestedInteger[];
}
class NestedIterator {
private list: number[] = [];
private index: number = 0;
constructor(nestedList: NestedInteger[]) {
this.flatArray(nestedList);
}
private flatArray(arr: NestedInteger[]): void {
for (let i = 0; i < arr.length; i++) {
if (arr[i].isInteger()) {
this.list.push(arr[i].getInteger());
} else {
this.flatArray(arr[i].getList());
}
}
}
hasNext(): boolean {
return this.index < this.list.length;
}
next(): number {
if (this.hasNext()) {
return this.list[this.index++];
}
throw new Error('No more elements');
}
}
// 辅助函数 flatArray 的 TypeScript 版本
function flatArray(arr: any[]): any[] {
const stack = [...arr];
const ret: any[] = [];
while (stack.length) {
let next = stack.pop();
if (Array.isArray(next)) {
stack.push(...next);
} else {
ret.unshift(next); // 使用 unshift 来保持原始顺序
}
}
return ret;
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* var obj = new NestedIterator(nestedList)
* var a: number[] = []
* while (obj.hasNext()) a.push(obj.next());
*/
302



被折叠的 条评论
为什么被折叠?



