题目: Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path.
In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period … moves the directory up a level. For more information, see: Absolute path vs relative path in Linux/Unix
Note that the returned canonical path must always begin with a slash /, and there must be only a single slash / between two directory names. The last directory name (if it exists) must not end with a trailing /. Also, the canonical path must be the shortest string representing the absolute path.
Example 1:
Input: “/home/” Output: “/home” Explanation: Note that there is no trailing slash after the last directory name. Example 2:
Input: “/…/” Output: “/” Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go. Example 3:
Input: “/home//foo/” Output: “/home/foo” Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one. Example 4:
Input: “/a/./b/…/…/c/” Output: “/c” Example 5:
Input: “/a/…/…/b/…/c//.//” Output: “/c” Example 6:
Input: “/a//bc/d//././/…” Output: “/a/b/c”
绝对路径和相对路径的区别 见ABSOLUTE PATH VS RELATIVE PATH IN LINUX/UNIX
Example 4: My present location is /etc/lvm and I want to change my location to /opt/oradba
Using relative path:
cd …/…/opt/oradba
解法: 首先我们将目标字符串按照"/“来分割 由于字符串第一个字符为”/",所以会切割出来一个空字符串"" 然后依次进行处理,如果是空字符串或者是".",代表当前目录,我们直接可以忽略不管 如果是"…",代表是父亲目录,我们直接去除我们上一个加入的目录,此时我们要注意如果此时没有目录添加进来,我们是不能去除上一层目录,我们可以使用出栈操作, 在c++中可以使用vector中的pop_back()操作,在java中可以使用LinkedList的removeLast来去除最后一个元素 剩下的情况,我们直接添加目录即可,但是我们要判断此时的字符串不是"…",如果字符串为"…“而目录不为空,直接我们在之前的if条件中已经判断过了,但是目录为空的条件下,如果我们不加判断字符串不是”…“会将”…"错误的加入到目录中去。
如:"/a/./b/…/…/c/" 分割的第一个为空字符串忽略 分割的第二个为a,加入列表a 分割的第三个为. 忽略 分割的第四个为b,加入列表a,b 分割的第四个为…, 这代表父亲目录,即当前目录为b,b的父亲目录为a,所以我们要去除b目录,列表a 分割的第五个为…, 这代表父亲目录,即当前目录为a,b的父亲目录为根目录,所以我们要去除a目录,列表为空、 分割的第六个为c,加入列表为c
然后我们将列表中的元素用"/“连接起来,注意我们要在最前面加上”/" c++: c++中没有连接函数,我们只有遍历列表,然后在每个元素前加上"/",最后如果列表为空,我们直接输出"/"就可以了 c++中用stringstream类来实现分割功能,getline(ss,t,’/’)函数最后一个参数是字符
class Solution { public: string simplifyPath(string path) { string res,t; stringstream ss(path); vector<string> v; while(getline(ss,t,'/')){ cout << t + "hahaha" << endl; if(t == "" || t == ".") continue; if(t == ".." && !v.empty()) v.pop_back(); else if(t != "..") v.push_back(t); } for(string s : v){ res += "/" + s; } return v.empty() ? "/" : res; } };java: split函数是分割,String.join函数是连接,有两个参数,第一个是连接符,第二个是连接的列表
class Solution { public String simplifyPath(String path) { LinkedList<String> v = new LinkedList<>(); String[] paths = path.split("/"); for(String t : paths){ //System.out.println(t + "hahaha"); if(t.equals("") || t .equals(".") ) continue; if(t.equals("..") && !v.isEmpty()) v.removeLast(); else if(!t.equals("..")) v.add(t); } String res = String.join("/",v); //System.out.println(v); return "/" + res; } }python: 如果不用continue,可以对条件取非,再加上if条件 如在本例中, if(t == “” || t == “.”) continue; 可以改为if t != “” and t != “.”
python中的连接函数为 连接符.join() 参数为列表
class Solution(object): def simplifyPath(self, path): """ :type path: str :rtype: str """ paths = [p for p in path.split("/") if p != "" and p!= "."] res = [] for p in paths: if p == ".." and res: res.pop() elif p != "..": res.append(p) return "/" + "/".join(res)