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 ...
随机推荐
- BCP导出导入大容量数据实践
前言 SQL SERVER提供多种不同的数据导出导入的工具,也可以编写SQL脚本,使用存储过程,生成所需的数据文件,甚至可以生成包含SQL语句和数据的脚本文件.各有优缺点,以适用不同的需求.下面介绍大 ...
- UIScrollView的基本使用
UIScrollView的用法很简单 将需要展示的内容添加到UIScrollView中 设置UIScrollView的contentSize属性,告诉UIScrollView所有内容的尺寸,也就是告诉 ...
- C# 的界面控件属性修改线程安全问题
今天在实验delegate与thread 在初步的实验结束后,因为原来的delegate只有一个函数会被调用,感觉没有达到delegate的极致,所以又重新自己定义了一个delegate,在另一个线程 ...
- UVALive 3644 X-Plosives
X-Plosives Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Submit] ...
- UI第十二节
- (void)viewDidLoad { [super viewDidLoad]; UIButton *btn = [UIButton buttonWithType:UIButt ...
- PHP中curl的CURLOPT_POSTFIELDS参数使用细节
CURL确实是一个不错的好工具,不仅在PHP中还是其他的操作系统中,都是一个非常好用的.但是如果你有些参数没有用好的话,那可能会得不到自己理想中的结果. 在通常情况下,我们使用 CURL 来提交 PO ...
- java基础 字符串 “==” 和 “equals” 比较
demo: public class TestStringEquals { public static void main(String[] args) { String a = "test ...
- javascript数据结构-链表
gihtub博客地址 链表 是一种物理存储单元上非连续.非顺序的存储结构,它既可以表示线性结构,也可以用于表示非线性结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每 ...
- js倒计时代码 适合于促销-倒计时代码
<div class="tiem_price clearfix fonts" style="margin-top:15px;"> <div c ...
- column css3 列宽
column-count 属性规定元素应该被分隔的列数: div { -moz-column-count:3; /* Firefox */ -webkit-column-count:3; /* Saf ...