class Solution {
public:
string simplifyPath(string path) {
string ans="";
vector<string>p;
int i=0;
while(i<path.size())
{
while(i<path.size()&&path[i]=='/')i++;
if(i==path.size())break;
string temp;
while(i<path.size()&&path[i]!='/')
temp+=path[i],i++;
if(temp==".")continue;
else if(temp=="..")
{
if(p.size())p.pop_back();
}
else p.push_back(temp);
}
for(int i=0;i<p.size();i++)ans=ans+'/'+p[i];
if(ans.size()==0)ans="/";
return ans;
}
};
class Solution {
public:
string simplifyPath(string path) {
string ans;
stack<string>st;
int i=0;
while(i<path.size())
{
while(i<path.size()&&path[i]=='/')i++;
if(i==path.size())break;
string temp="";
while(i<path.size()&&path[i]!='/')
temp+=path[i],i++;
if(temp==".")continue;
else if(temp=="..")
{
if(!st.empty())st.pop();
}
else st.push(temp);
}
while(!st.empty())
ans='/'+st.top()+ans,st.pop();
if(ans=="")ans="/";
return ans;
}
};
simplify-path
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 1 stack存字母,遇到..就pop一个出去 2 大部分重复的/是没用的,所以先把所有/去掉 3 .也是没用的 ...
- 题目链接https://oj.leetcode.com/problems/simplify-path/ Descr...
- 问题 Given an absolute path for a file (Unix-style), simpli...