题解 | #栈和排序#
栈和排序
https://www.nowcoder.com/practice/95cb356556cf430f912e7bdf1bc2ec8f
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 栈排序 * @param a int整型一维数组 描述入栈顺序 * @return int整型一维数组 */ function solve( a ) { // write code here if (!a || a.length === 0) return [] let sortA = Object.assign([], a); sortA.sort((a, b) => b - a); let stack = [], ans = [], i = 0, j = 0, max = sortA[0]; while(j < a.length) { stack.push(a[j]); j++; while (stack.length && stack[stack.length - 1] === max) { ans.push(stack.pop()); let s = a.slice(j).sort((a,b)=>b-a); max = Math.max(s[0], stack[stack.length - 1] || Number.MIN_SAFE_INTEGER); } } while(stack.length) { ans.push(stack.pop()); } return ans; } module.exports = { solve : solve };