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/", ...
随机推荐
- <转>32位移植到64位 注意事项
32bit-64bit porting work注意事项 64位服务器逐步普及,各条产品线对64位升级的需求也不断加大.在本文中,主要讨论向64位平台移植现有32位代码时,应注意的一些细小问题. 什么 ...
- C#日志写入
/// <summary> /// 写日志,指定日志文件 /// </summary> /// <param name="File"></ ...
- 【转载】OGRE 内存管理
原文:OGRE 内存管理 Ogre引擎中与内存管理相关的文件大致有以下几个(只列出头文件) OgreAlignedAllocator.h OgreMemoryAllocatedObject.h Ogr ...
- 如何查看postfix的版本
[root@localhost sasl2]# postconf -d | grep mail_versionmail_version = 2.10.1milter_macro_v = $mail_n ...
- 列联表(Crosstabs)
四格表(2*2的列联表): Tmin为最小的频数:N为频数之和. 1 当 Tmin≥5,N≥40时, 用普通卡方检验公式;2 当1≦Tmin≦5, N≥40时, 用校正卡方检验公式;3 Tmin< ...
- POJ 1321棋盘问题
题意: 给定一个n*n(n<=8)的棋盘, 有些地方能放棋子, 有些不能. 放m个棋子, 求能使这m个棋子不同行且不同列的方案数. 分析: 用一个一维数组标记行和列, 深搜一下. #includ ...
- 用一张图片制作skybox图片 (如何制作360全景图、立方体)
我转发的帖子在这里 http://dong2008hong.blog.163.com/blog/static/4696882720140294039205/ 原帖不知道地址 我这里简单整理了下 去掉了 ...
- 个人博客作业WEEK 1
一.项目时间规划与实际用时 PSP2.1 Personal Software Process Stages 预计时间/h 实际时间/h Planning 计划 · Estimate · 估计这个任 ...
- Linux下C程序的编译,运行,及调试
先查看linux有没有gcc 和 gdb $ gcc -v $ gdb -v 如果没有安装gcc,可以 $ yum install gcc 要获取管理员权限才能安装软件,$ su root (有的li ...
- html块级元素和内联元素小结
Block element 块级元素 顾名思义就是以块显示的元素,高度宽度都是可以设置的.比如我们常用的<div>.<p>.<ul>默认状态下都是属于块级元素.块级 ...