leetcode-Simplify Path
来源:互联网 发布:美少女万华镜 mac 编辑:程序博客网 时间:2024/05/17 08:49
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
思路:字符串的处理,依然考虑双指针,
用vector实现栈的功能:
遇到.不变;
遇到..如果vector不为空,则弹出最后一个,
其他,压入vecort;
代码:
string simplifyPath(string path) {
vector<string> tempVector;
string res;
int len=path.length();
int i=0;
string temp="";
int start=0;
int end=0;
while(start < len)
{
end=start;
while(end<len && path[end]!='/')
{
end++;
}
temp=path.substr(start,end-start);
if(temp==".."&&!tempVector.empty())
tempVector.pop_back();
else if(temp!="" && temp!="."&&temp!="..")
tempVector.push_back(temp);
start=end+1;
}
if(tempVector.size() == 0)
{
res="/";
return res;
}
int j=0;
int count=tempVector.size();
while(j<count)
{
res=res+"/";
res=res+tempVector[j];
++j;
}
return res;
}
- LeetCode: Simplify Path
- LeetCode Simplify Path
- LeetCode : Simplify Path
- [Leetcode] Simplify Path
- [leetcode] simplify path
- [LeetCode] Simplify Path
- leetcode 100: Simplify Path
- [leetcode]Simplify Path
- [LeetCode]Simplify Path
- [leetCode] Simplify Path
- [leetcode]Simplify Path
- [leetcode] Simplify Path
- [LeetCode] Simplify Path
- leetcode simplify path
- Leetcode: Simplify Path
- LeetCode题解:Simplify Path
- [LeetCode] Simplify Path
- LeetCode Simplify Path
- 恢复Oracle删除的数据
- 协议森林:先生,要点单吗?---HTTP协议概览
- 监控硬盘和普通硬盘有多大差别?
- com.sun.net.httpserver内存泄露的问题
- 隐马尔科夫
- leetcode-Simplify Path
- Maven的安装、配置及使用入门
- centos 安装 网络安装使用本地镜像文件安装
- 被架构师渺杀之后。。。
- 【会议致辞必备语 】
- 结构体和联合体的区别
- Hadoop Ecosystem Map
- C++中指针的复制
- Android最全的开源资源整理