Leetcode 71 Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
path = "/a/../../b/../c//.//"
, => "/c"
path = "/a//b////c/d//././/.."
, => "/a/b/c"
In a UNIX-style file system, a period ('.') refers to the current directory, so it can be ignored in a simplified path. Additionally, a double period ("..") moves up a directory, so it cancels out whatever the last directory was. For more information, look here: https://en.wikipedia.org/wiki/Path_(computing)#Unix_style
Corner Cases:
- Did you consider the case where path =
"/../"
?
In this case, you should return"/"
. - Another corner case is the path might contain multiple slashes
'/'
together, such as"/home//foo/"
.
In this case, you should ignore redundant slashes and return"/home/foo"
.
這個題的意思是仿照Unix的路徑規劃來進行模擬
class Solution {
public:
string simplifyPath(string path) {
path += '/';//首先加上'/'以簡化操作
string res,s;
for(auto c : path){
if(res.empty()){//res為實際的串
res += c;
}else if(c != '/'){//讀入正常的串(兩個'/'之間的串)
s += c;
}else{
if(s == ".."){
if(res.size() > 1){
res.pop_back();
}
while(res.back() != '/'){//連續退,主要是在兩個'/'為一段很長的串的時候
res.pop_back();
}
}else if(s != "" && s != "."){//正常新增
res += s + '/';
}
s = "";//將這個串清零
}
}
if(res.size() > 1){
res.pop_back();//去掉'/'
}
return res;
}
};
相關文章
- LeetCode 71. Simplify PathLeetCode
- Leetcode Path SumLeetCode
- LeetCode 112. Path SumLeetCode
- Leetcode 329. Longest Increasing Path in a MatrixLeetCode
- leetcode388. Longest Absolute File PathLeetCode
- leetcode-124-Binary Tree Maximum Path SumLeetCode
- LeetCode 124. Binary Tree Maximum Path SumLeetCode
- Leetcode 329. Longest Increasing Path in a Matrix (python+cpp)LeetCodePython
- Flutter Path(二) : Path 進階Flutter
- Flutter Path(一) : Path 與 CustomPainterFlutterAI
- [BUG反饋]defined('ADDON_PATH') or define('ADDON_PATH', APP_PATH.'Addon');APP
- crontab on raspberry pi, full path, not relative path, is needed.
- BUUCTF-WEB(71-75)Web
- BUUCTF-Misc(71-80)
- Path-sum
- Path Sum III
- Longest Univalue Path
- Python(Path().name)Python
- __dirname, __filename, path.resolve, path.join, process.cwd
- [譯] Chrome 71 新特性介紹Chrome
- 牛客練習賽 71 AC
- mac下的LD_LIBRARY_PATH是DYLD_LIBRARY_PATHMac
- CSS clip-pathCSS
- 112-Path Sum
- Cookie path 屬性Cookie
- DriveInfo類,Path類
- The Staff Engineer’s Path
- os.path.split
- python技巧-使用os.path.join和os.path.sep.joinPython
- Chrome 71 新特性[雙語+視訊]Chrome
- 64. Minimum Path Sum
- 687-Longest Univalue Path
- 437-Path Sum III
- 113-Path Sum II
- NodeJS之path模組NodeJS
- Path 突破Canvas極限Canvas
- os.path()模組
- Please provide a valid cache pathIDE