We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
//输入以下有序的list数组 var list = [ 'h3', 'h2', 'h3', 'h1', 'h2', 'h3', 'h3', 'h2', 'h3', 'h1', 'h2', 'h4', 'h2', 'h3',s ] //输出以下树形数组结构 [ { name: 'h3', child: [] }, { name: 'h2', child: [{name: 'h3'}] }, { name: 'h1', child: [ { name: 'h2', child: [{name: 'h3'}, {name: 'h3'}] }, { name: 'h2', child: [{name: 'h3'}] } ] }, { name: 'h1', child: [ { name: 'h2', child: [{name: 'h4'}] }, { name: 'h2', child: [{name: 'h3'}] } ] } ]
let stacks = []; function createNode(key) { return { name: key, child: [] } } function task(node, key) { stacks.push({ name: key, node: node }) } function Compare(k1, k2) { return Number(k1.slice(1)) <= Number(k2.slice(1)) } function insert(el) { let _t = stacks[stacks.length - 1]; if (!stacks.length) { let newnode = createNode(el); result.push(newnode); task(newnode, el); } else { if (Compare(el, _t.name)) { stacks.pop(); insert(el) } else { let newnode = createNode(el); _t.node.child.push(newnode); task(newnode, el); } } } let list = [] let total = 1000000 for(let i = 0; i < total; i++) { list.push('h' + Math.floor(Math.random() * 10 + 1)) } let result = [] list.forEach(insert);
The text was updated successfully, but these errors were encountered:
stacks的作用是:
Sorry, something went wrong.
No branches or pull requests
题目
思路
The text was updated successfully, but these errors were encountered: