NC299. 简化目录路径
描述
示例1
输入:
"/home/web/"
输出:
"/home/web"
示例2
输入:
"/home////web//"
输出:
"/home/web"
说明:
在简化规范路径中,多个连续的"/"替换为"/",示例3
输入:
"/../"
输出:
"/"
说明:
"/"是根目录,是顶级目录,它的上级目录还是自己示例4
输入:
"/home/web/./tang/../miao/"
输出:
"/home/web/miao"
说明:
"/home/web/./"表示的是"/home/web",因为"."表示当前目录,但是"/home/web/./tang/../"表示的还是"/home/web",因为".."表示父目录,"/home/web/tang"的父目录就是"/home/web"C++ 解法, 执行用时: 7ms, 内存消耗: 1036KB, 提交时间: 2022-07-26
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param path string字符串 * @return string字符串 */ string simplifyPath(string path) { // write code here string a, b; if (path.back() != '/') path += '/'; for (auto c : path) { if (c != '/') b += c; else { if (b == "..") { while (a.size() && a.back() != '/') a.pop_back(); if (a.size()) a.pop_back(); } else if (b != "." && b != "") { a += "/" + b; } b.clear(); } } if (a.empty()) a = "/"; return a; } };
C 解法, 执行用时: 7ms, 内存消耗: 1044KB, 提交时间: 2022-06-25
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param path string字符串 * @return string字符串 * * C语言声明定义全局变量请加上static,防止重复定义 */ char* simplifyPath(char* path ) { int stackStart[100001], stackEnd[100001], sTop = 0; int pLen = strlen(path); char tmp[100001] = ""; int tmpIndex = 0; for (int i = 1; i < pLen; i++) { tmpIndex = 0; int j = i; for (; j < pLen; j++) { if (path[j] != '/') { tmp[tmpIndex++] = path[j]; } else { break; } } if (tmpIndex != 0) { tmp[tmpIndex] = '\0'; if (strcmp(tmp, "..") == 0) { if (sTop > 0) { sTop--; } } else if (strcmp(tmp, ".") == 0) { // do nothing } else { stackStart[sTop] = i; stackEnd[sTop] = j - 1; sTop++; } } i = j; } tmpIndex = 0; int start, end; for (int i = 0; i < sTop; i++) { tmp[tmpIndex++] = '/'; start = stackStart[i]; end = stackEnd[i]; for (int j = start; j <= end; j++) { tmp[tmpIndex++] = path[j]; } } if (tmpIndex == 0) { tmp[tmpIndex++] = '/'; } tmp[tmpIndex] = '\0'; char *result = (char *)malloc(sizeof(char) * (tmpIndex + 1)); strcpy(result, tmp); return result; }
C++ 解法, 执行用时: 8ms, 内存消耗: 1036KB, 提交时间: 2022-08-04
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param path string字符串 * @return string字符串 */ string simplifyPath(string path) { // write code here string a, b; if (path.back() != '/') path += '/'; for (auto c : path) { if (c != '/') b += c; else { if (b == "..") { while (a.size() && a.back() != '/') a.pop_back(); if (a.size()) a.pop_back(); } else if (b != "." && b != "") { a += "/" + b; } b.clear(); } } if (a.empty()) a = "/"; return a; } };
C++ 解法, 执行用时: 9ms, 内存消耗: 1024KB, 提交时间: 2022-06-15
class Solution { public: string simplifyPath(string path) { string a, b; if (path.back() != '/') path += '/'; for (auto c : path) { if (c != '/') b += c; else { if (b == "..") { while (a.size() && a.back() != '/') a.pop_back(); if (a.size()) a.pop_back(); } else if (b != "." && b != "") { a += "/" + b; } b.clear(); } } if (a.empty()) a = "/"; return a; } };
C++ 解法, 执行用时: 9ms, 内存消耗: 1024KB, 提交时间: 2022-03-07
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param path string字符串 * @return string字符串 */ string simplifyPath(string path) { string res, name; if(path.back() != '/') path += '/'; for(auto c : path) { if(c != '/') name += c; else { if(name == "..") { while(res.size() && res.back() != '/') res.pop_back(); if(res.size()) res.pop_back(); } else if(name != "." && name != "") { res += "/" + name; } name.clear(); } } if(res.empty()) res = "/"; return res; } };