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 Simplify PathLeetCode
- LeetCode-Simplify PathLeetCode
- Simplify Path leetcode javaLeetCodeJava
- Leetcode Path SumLeetCode
- Leetcode Path Sum IILeetCode
- Leetcode-Path SumLeetCode
- Path Sum leetcode javaLeetCodeJava
- Leetcode Minimum Path SumLeetCode
- Leetcode-Path Sum IILeetCode
- Path Sum II leetcode javaLeetCodeJava
- LeetCode- Longest Absolute File PathLeetCode
- Leetcode-Minimum Path SumLeetCode
- Minimum Path Sum leetcode javaLeetCodeJava
- LeetCode 112. Path SumLeetCode
- leetcode388. Longest Absolute File PathLeetCode
- LeetCode-Longest Increasing Path in a MatrixLeetCode
- Leetcode Binary Tree Maximum Path SumLeetCode
- Leetcode 329. Longest Increasing Path in a MatrixLeetCode
- Leetcode-Bianry Tree Maximum Path SumLeetCode
- Binary Tree Maximum Path Sum leetcode javaLeetCodeJava
- 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
- 【LeetCode從零單排】No112 Path SumLeetCode
- Flutter Path(二) : Path 進階Flutter
- Flutter Path(一) : Path 與 CustomPainterFlutterAI
- Script: Script to Simplify the Use of Explain Plan (Doc ID 1019631.6)AI
- Conventional Path Export和Direct Path ExportExport
- Conventional Path Export Versus Direct Path ExportExport
- sga contains infomation (71)AI
- BUUCTF-Misc(71-80)
- BUUCTF-WEB(71-75)Web
- crontab on raspberry pi, full path, not relative path, is needed.
- [譯] Chrome 71 新特性介紹Chrome
- Oracle for windows - pathOracleWindows
- Java Path (轉)Java
- [BUG反饋]defined('ADDON_PATH') or define('ADDON_PATH', APP_PATH.'Addon');APP