1 回答

TA貢獻1785條經(jīng)驗 獲得超4個贊
通過一些抽象,您可以得到一個節(jié)點是塊的圖。
其中一些是微不足道的葉子。
您只想對圖表進行 dfs,直到找到與您的路徑字符串匹配的塊
function dfs(block, path) {
if (block.getId() === path) return block
let found = null
block.getRows().find(r => r.getCells().find(bContent => {
let b = bContent.getContent()
found = dfs(b, path)
return found
}))
return found
}
堆棧的變化
function dfs(block, path) {
const stack = [block]
while (stack.length) {
const b = stack.pop()
if (b.getId() === path) return block
b.getRows().forEach(r => r.getCells().forEach(bContent => {
let b = bContent.getContent()
stack.push(b)
})
}
return null
}
添加回答
舉報