直接放上题目leetcode71
题目主要是如何提取/和/之间的子串,强行做事可以的,但是很麻烦,所以下面直接放出代码:
class Solution {
public:
string simplifyPath(string path) {
vector<string>st;
string tmp, res = "";
stringstream ss(path);
while(getline(ss, tmp, '/')){
if(tmp == "" || tmp == ".") continue;
if(tmp == ".." && !st.empty()) st.pop_back();
else if(tmp != "..") st.push_back(tmp);
}
for(string s : st) res += "/" + s;
return res == "" ? "/" : res;
}
};
这里是用了stringstream这个类,然后用getline这个方法提取,使得题目变得简单。