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 ...
随机推荐
- CLion 2016.1.1 下载 附注册激活码 破解版方法
http://www.520xiazai.com/soft/CLion-2016.1.1.html CLion 2016.1.1 下载 附注册激活码 破解版方法 注册破解方法:在要求输入注册的界面选择 ...
- Protecting against XML Entity Expansion attacks
https://blogs.msdn.microsoft.com/tomholl/2009/05/21/protecting-against-xml-entity-expansion-attacks/ ...
- PHP函数 addslashes() 和 mysql_real_escape_string() 的区别 && SQL宽字节,绕过单引号注入攻击
首先:不要使用 mysql_escape_string(),它已被弃用,请使用 mysql_real_escape_string() 代替它. mysql_real_escape_string() 和 ...
- Asp.Net Core--基于角色的授权
翻译如下: 当创建身份时,它可以属于一个或多个角色,例如Tracy可以属于管理员和用户角色,而Scott可以仅属于用户角色. 如何创建和管理这些角色取决于授权过程的后备存储. 角色通过ClaimsPr ...
- JS学习:第一周——NO.1预解释
1.何为预解释? 在当前作用域下,在JS代码执行之前,浏览器会对带var和带function的进行提前声明或定义: ①带var的:只声明不定义:告诉浏览器,有这么一个变量,但是并没有赋值 ②带func ...
- php文件类
1.需求 了解php对文件的一些操作 2.例子 写了一个类,可以操作文件,包含增,删,查 <?php class myfile{ public function write_file($stri ...
- Hibernate一对一、一对多、多对多注解映射配置
一对一: 一对多: 多对多:
- JavaScript之bind,call,apply
参考: http://rangercyh.blog.51cto.com/1444712/1615809 function foo(a,b) { this.x = this.x + a + b; } / ...
- vmware esxi 找不到网卡驱动,硬盘的解决方法
解决方法就是把ESXi无法识别的硬件的驱动定制进安装镜像文件中. ESXi 5.5 U2: VMware-VMvisor-Installer-5.5.0.update02-2068190.x86_64 ...
- java 深入技术四(Set)
1)Set接口 set接口的父接口-Collection set接口的重要子类-HashSet set接口的重要子类 -TreeSet set 接口的特别子类-LinkedHashSet 2)Hash ...