MCPcopy Create free account
hub / github.com/OBKoro1/web-basics / simplifyPath

Function simplifyPath

src/leetCode/simplify-path/simplify-path.js:1–22  ·  view source on GitHub ↗
(path)

Source from the content-addressed store, hash-verified

1function simplifyPath(path) {
2 let stack = path.split('/') // 根据/切割 转成栈
3 let res = [] // 目录结果集
4 // 从目录开始 循环栈
5 for (let i = 0; i < stack.length; i++) {
6 let item = stack[i]
7 if (item === '' || item === '.') {
8 // .以及多个/不操作
9 continue
10 } else if (item === '..') {
11 if (res.length !== 0) {
12 res.pop() // 前一个目录出栈
13 }
14 } else {
15 // 添加目录到后面
16 res.push(item)
17 }
18 }
19 // 拼接/符号
20 const result = `/${res.join('/')}`
21 return result
22}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected