[LeetCode] Simplify Path,文件路径简化,用栈来做
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
- 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"
.
如果仅仅从不断replace输入路径的角度出发,会非常复杂。
如果用一个栈来一次存储各级路径的directory名字,然后重组,会简便一些,这也是文件路径简化类题目的常用思路。
为了末尾可以顺序遍历栈重组path,我们不用传统的stack lib,而用vector来实现栈,这样可以方便顺序遍历。
代码:
class Solution {
public:
string simplifyPath(string path) {
if(path.length() == ) return "";
vector<string> v;
int p = , start = ;
while(p < path.length()){
if(path[p] == '/') ++p;
else{
start = p;
while(p < path.length() && path[p] != '/') ++p;
string temp = path.substr(start, p-start);
if(temp == ".."){
if(!v.empty()) v.pop_back(); //遇到".."就出栈
else{
if(path[] != '/') v.push_back(temp); //如果没东西可以出,就要看path是否以根目录开头了,不是以根目录开头的话,".."要进栈的
}
}else if(temp == "."){} //遇到"."就跳过
else if(temp.length() > ){
v.push_back(temp);
}
}
}
string res = (path[] == '/' ? "/" : ""); //重组path
for(vector<string>::iterator i = v.begin(); i < v.end(); ++i){
res.append(*i);
if(i < v.end()-) res.append("/");
}
return res;
}
};
[LeetCode] Simplify Path,文件路径简化,用栈来做的更多相关文章
- Simplify Path(路径简化)
问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [leetcode]Simplify Path @ Python
原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-sty ...
- [LeetCode] Coin Path 硬币路径
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...
- System.IO.Path文件路径类
Path类的静态属性和方法,此类操作不影响物料文件. 属性 char a = System.IO.Path.VolumeSeparatorChar;//: char b = System.IO.Pat ...
- Leetcode Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [LeetCode] Simplify Path(可以不用看)
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- [leetcode]112. Path Sum路径和(是否有路径)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- JQuery常用函数方法全集
Attribute: $("p").addClass(css中定义的样式类型); 给某个元素添加样式 $("img").attr({src:"test ...
- ARKit----学习一
一.ARKit的简介 开始进入正题吧 ARKit在iOS 11上推出的一个AR移动平台,支持A9以上的处理器,不支持模拟器.ARKit使用相机捕捉现实世界,使用SceneKit,SpriteKit或者 ...
- hive mapjoin优化
默认为10MB,如果大于该值不会执行mapjoin,hive语句中直接设置的mapjoin也不再起作用. 参考hive wiki把hive.auto.convert.join.noconditiona ...
- Sqoop使用笔记(转载)
Sqoop是Apache顶级项目,主要用来在Hadoop和关系数据库中传递数据.通过sqoop,可以方便的将数据从关系数据库导入到HDFS,或将数据从HDFS导出到关系数据库. 关于Sqoop 官网S ...
- 【PHP】- PHPStorm+XDebug进行调试图文教程
转载:https://www.cnblogs.com/LWMLWM/p/8251905.html 这篇文章主要为大家详细介绍了PHPStorm+XDebug进行调试图文教程,内容很丰富,具有一定的 ...
- 第三章 持续集成jenkins工具使用之邮件配置
1 Email Extension Plugin插件安装 持续集成很重要的一环就是及时将构建结果通知到对应的责任人,如:构建失败了,至少需要下发通知给造成本次构建失败的开发人员,如果包含自动化测试 ...
- Redis的概述和简单使用(转载)
文章来源:http://jingyan.baidu.com/article/db55b60996d0124ba30a2f92.html Redis是一个基于key-value的高速缓存系统,类似于me ...
- Bootstrap 栅格系统初识
以下理论内容copy自bootstrap中文网(一个不错的bootstrap学习网站). 栅格系统 Bootstrap 提供了一套响应式.移动设备优先的流式栅格系统,随着屏幕或视口(viewport) ...
- MVC 上传文件实例
http://www.cnblogs.com/leiOOlei/archive/2011/08/17/2143221.html
- BZOJ 1786 配对(DP)
如果我们直接令dp[i][j]为前i个位置第i个位置填j所产生的逆序对的最少数.这样是不满足无后效性的. 但是如果发现对于两个-1,如果前面的-1填的数要大于后面的-1填的数.容易证明把他们两交换结果 ...