[71] 简化路径

 1 /**
 2  * @param {string} path
 3  * @return {string}
 4  */
 5 var simplifyPath = function (path) {
 6   const arr = path.split('/');
 7   const ans = [];
 8   for (const item of arr) {
 9     if (item === '..') {
10       ans.pop();
11     } else if (item && item !== '.') {
12       ans.push(item);
13     }
14   }
15   return '/' + ans.join('/');
16 };

 

posted @ 2023-12-22 15:57  人恒过  阅读(12)  评论(0)    收藏  举报