标签:
题目:
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
我的解答:
/** * @param {string} path * @return {string} */ var simplifyPath = function(path) { var smiplify = ""; var array = path.split(‘/‘); var deque = []; for (var i = 0; i < array.length; i++) { str = array[i]; if(str === ‘‘ || str === ‘.‘) { continue; } else if (str === "..") { deque.pop(); } else { deque.push(str); } } smiplify = "/"; var strDeque = deque.join(‘/‘); console.log("log:" + strDeque); if (strDeque[0] === ‘/‘) { smiplify = strDeque; } else { smiplify = ‘/‘ + strDeque; } console.log("log:" + smiplify); return smiplify; };
答题心得: 注意最简单的字符串题目,注意split的用法。
标签:
原文地址:http://www.cnblogs.com/ender-cd/p/4607540.html