LeetCode : 71. Simplify Path — JavaScript solution
Published in
Jun 28, 2022
你領500元現金了嗎? 點選我的連結成功開樂天帳戶+登入樂天網銀APP,拿500元現金! 樂天帳戶好康在這 : · 使用行動支付5次,享次月活儲年息1.35%存額無上限 · VIP享每月免費跨提/轉共16次 · 提領日幣手續費優惠8次/月 (推薦序號: JGONGL)
var simplifyPath = function(path) {
let arr = path.split('/')
let stack = []
for(e of arr) {
if(e === '..') stack.pop()
else if(e !== '.' && e !== '') stack.push(`/${e}`)
}
return stack.length ? stack.join('') : '/'
};
利用 stack 的概念,先進後出,判斷 stack 長度,並集合 stacky