LeetCode : 71. Simplify Path — JavaScript solution

Gary Huang
Dublin so code
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

台灣百岳 嘉明湖 登山 羊文學 — more than words / FunkyGary / Taiwan — Jiaming Lake

--

--

Gary Huang
Dublin so code

Self-taught in programming, currently working as a web developer and also a online course instructor, helping self-taught programmers find employment.