71. Simplify Path 发表于 2016-12-15 | 分类于 LeetCode | | 阅读次数: 题目链接! 解题思路 使用一个栈去存储每一条路径,如果是..就pop(); 注意处理边界/, /., /..; 代码12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455class Solution{ public: string simplifyPath(string path) { string res, temp; res = temp = ""; stack<string> st; int len = path.size(); for(int i = 0; i < len; ++i) { if(path[i] == '/') { if(temp == "" || temp == ".") { temp = ""; continue; } else if(temp == "..") { temp = ""; if(!st.empty()) st.pop(); } else { st.push(temp); temp = ""; } } else { temp += path[i]; } } if(temp != "") { if(temp == "..") { if(!st.empty()) st.pop(); } else if(temp != ".") st.push(temp); } if(st.empty()) return "/"; while(!st.empty()) { res = "/" + st.top() + res; st.pop(); } return res; }}; -------------本文结束感谢您的阅读-------------