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"
.
题目不难,主要考虑一些特殊情况。
对于path = "/a/./b/../../c/"
, => "/c",模拟一下
先按照'/'对字符串进行分割,得到 [a, . , b, .. , .. , c]
首先进入目录a,注意 '.' 代表当前目录 ,".."代表上一个目录
然后到达'.',还是在当前目录,/a
然后到达'b',这为/a/b
然后到达'..',这是回到父目录,则变为/a
然后到达'..',继续回到父目录,则变为/
然后到达'c',则达到子目录,变为/c
class Solution {
public:
vector<string> split(string& path, char ch){
int index = ;
vector<string> res;
while(index < path.length()){
while(index < path.length() && path[index] == '/') index++;
if(index >= path.length()) break;
int start=index, len = ;
while(index < path.length() && path[index]!='/') {index++;len++;}
res.push_back(path.substr(start,len));
}
return res;
} string simplifyPath(string path) {
vector<string> a = split(path,'/');
vector<string> file;
for(int i = ; i < a.size(); ++ i){
if(a[i] == ".." ){
if(!file.empty()) file.pop_back();
}
else if(a[i]!=".") file.push_back(a[i]);
}
string res="";
if(file.empty()) res ="/";
else{
for(int i = ; i < file.size(); ++ i) res+="/"+file[i];
}
return res;
}
};
Leetcode Simplify Path的更多相关文章
- [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] 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面试准备:Simplify Path
leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- 查看修改Linux时区和时间
查看/修改Linux时区和时间 一.时区 1. 查看当前时区 date -R 2. 修改设置时区 方法(1) tzselect 方法(2) 仅限于RedHat Linux 和 CentOS timec ...
- Apache Shiro 学习记录1
最近几天在学习Apache Shiro......看了一些大神们的教程.....感觉收获不少.....但是毕竟教程也只是指引一下方向....即使是精品教程,仍然有很多东西都没有说明....所以自己也稍 ...
- Spring中配置和读取多个Properties文件--转
public class PropertiesFactoryBeanextends PropertiesLoaderSupportimplements FactoryBean, Initializin ...
- DirectX SDK
http://blog.csdn.net/c4501srsy/article/details/17403927 http://blog.csdn.net/yy649487394/article/det ...
- Fragment间的通信
在网上看到的一篇文章,总结的很好 为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把 ...
- yii2事务运用举例
直接上代码: $db = Yii::$app->db; $transaction = $db->beginTransaction(); //开启事务 try { // 更新member表 ...
- fdatool 设计IIR滤波器
[B,A] = sos2tf(SOS);K = cumprod(G);k=K(end); [y_out] = filter(B, A, win_up_data, []) .*k;
- Redis Cluster搭建方法简介22211111
Redis Cluster搭建方法简介 (2013-05-29 17:08:57) 转载▼ Redis Cluster即Redis的分布式版本,将是Redis继支持Lua脚本之后的又一重磅 ...
- Unity3D 查找Update函数体为空的类
如果是大项目,有很多Update空跑还是多少有些效率损耗,那我们就把他们都找出来. 先引用Mono.Cecil //代码 using UnityEngine; using UnityEditor; u ...
- 关于Promise模式 整理中。。。
http://blog.csdn.net/womendeaiwoming/article/details/49849055 研究了几天Promise模式,因为在项目里也遇到了所谓的“回调陷阱”,就是多 ...