Simplify Path [LeetCode]
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"
.
Summary: Very careful about corner cases, like "/", "/.", "..".
class Solution {
public:
void handleEntry(vector<string> &path_stack, string path_entry) {
if(path_entry == "..") {
if(path_stack.size() > )
path_stack.pop_back();
}else if (path_entry != ".")
path_stack.push_back(path_entry);
} string simplifyPath(string path) {
string simple_path;
if(path.size() == )
return simple_path;
vector<string> path_stack;
string path_entry;
for(int i = ; i < path.size(); i ++) {
if(path_entry.size() == && path[i] == '/'){
continue;
}else if (path_entry.size() != && path[i] == '/') {
handleEntry(path_stack, path_entry);
path_entry.clear();
}else {
path_entry.push_back(path[i]);
}
}
if(path_entry.size() > )
handleEntry(path_stack, path_entry); for(string item: path_stack) {
simple_path += "/";
simple_path += item;
}
if(simple_path.size() == )
return "/";
return simple_path;
}
};
Simplify Path [LeetCode]的更多相关文章
- Simplify Path——LeetCode
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Simplify Path leetcode java
题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/&qu ...
- 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 ...
- 71. Simplify Path(M)
71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
随机推荐
- 【转载】Linux系统启动流程
原文:Linux系统启动流程 POST(Power On Self Test/上电自检)-->BootLoader(MBR)-->Kernel(硬件探测.加载驱动.挂载根文件系统./sbi ...
- POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
- WrapPanel流布局的一个简单例子
<Window x:Class="WrapPanel.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2 ...
- MQ使用几个命令
一.MQ常见基本指令: MQ现在使用的比较常见,但是在测试过程中,使用命令行直接查询,有时候事半功倍. activemq-admin stop activemq-admin list activemq ...
- Yii框架第一步-- 安装
0.首次安装请看 这里 下面的为非首次安装,不需要token的步骤 1.下载composer 官网下载: https://getcomposer.org/download/ 2.开启PHP的opens ...
- iOS - UISearchController
前言 NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearch ...
- 07 concurrency and Multi-version
本章提要---------------------------------------------------------对并发和锁的进一步补充并发控制事务的隔离级别多版本控制读一致性的含义写一致性- ...
- Python学习(10)元组
目录 Python 元组 访问元组 修改元组 删除元组 元组运算符 元组索引,截取 无关闭分隔符 元组内置函数 Python 元组 Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组 ...
- 在Windows上安装MySQL5.7
1. 下载安装包,这里选择压缩版mysql-5.7.16-winx64.zip: http://dev.mysql.com/downloads/mysql/ 2. 解压到安装目录,注意最好不要含有中文 ...
- caffe的data_reader.cpp分析一下干了点什么
首先说明:下面的内容不一定对 类body: 变量:LayerParameter param_ :它里面放的是:body传进来的layerparameter的参数: BlockingQueue<s ...