71. Simplify Path (Stack)
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/"
, => "/home"
path = "/a/./b/../../c/"
, => "/c"
注意:题目中已限定是absolute path - contains the root directory and all other subdirectories in which a file or folder is contained.
class Solution {
public:
string simplifyPath(string path) {
stack<string> pathStack;
string splitPath = path;
string curPath;
string ret="";
int pos = ;//absolute path, so path[0]=='/'
while(pos!=string::npos && pos+ < splitPath.length()){
splitPath = splitPath.substr(pos+);
pos = splitPath.find_first_of('/');
if(pos!=){ //ignore second '/'
curPath = splitPath.substr(,pos); if(curPath=="."){//ignore './' }
else if(curPath==".." ){ //ignore '/..'
if(!pathStack.empty()) pathStack.pop();
}
else{ //除了"."以及".."以外的都是文件名,比如"..."
pathStack.push(curPath);
}
}
} if(pathStack.empty()) return "/";
while(!pathStack.empty()){
ret = "/" + pathStack.top() + ret;
pathStack.pop();
}
return ret;
}
};
71. Simplify Path (Stack)的更多相关文章
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 71. Simplify Path(M)
71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...
- 【一天一道LeetCode】#71. Simplify Path
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [LC] 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...
- 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 71. Simplify Path压缩文件的绝对路径
[抄题]: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&q ...
- Leetcode#71 Simplify Path
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
随机推荐
- Thumbnailator 图像处理
Create a thumbnail from an image file Thumbnails.of(new File("original.jpg")) .size(160, 1 ...
- openresty websocket 使用
openresty websocket 使用 1. 代码如下: local server =require"resty.websocket.server" local wb, ...
- js核心知识
枚举属性: var o = {x:1} console.log("x" in o);//true console.log("toString" in o);// ...
- 2.Python输入pip命令出现Unknown or unsupported command 'install'问题解决
1.在学习python时,输入pip命令的时候出现以下错误: 2.原因:输入where pip命令查找,发现结果如下图,原因是因为电脑原先装了LoadRunner,导致系统无法识别应该使用哪一个pip ...
- Android 编译系统的组成
Android 和 Linux 的编译系统都是通过 Makefile 工具来组织编译源代码的. Makefile 工具用来解释和执行 Makefile 文件,在 Makefile 文件里定义好工程源代 ...
- 记一次socket_create()函数耗时异常记录
背景: 下午开发时突然整个页面耗时增加,空接口每次都需要2-3秒的耗时,一开始以为连开发环境数据库出现问题,最后断开数据库跑,发现还是很慢 最终逐步调试此页面耗时,定位到了socket_create( ...
- laravel路由定义
参考http://www.ruchee.com/notes/fms/laravel_primer.html 路由 路由定义位置在 app/routes.php 文件,支持五种路由方法,采用回调函数的形 ...
- qt在动态库里面加载widget的例子
testDll和testExe项目 备注:windows下dll内不需要new QApplication, linux和mac下面需要在动态库里面new QApplication testdll.h ...
- Java基础--NIO
NIO库在JDK1.4中引入,它以标准Java代码提供了高速的,面向块的IO,弥补了之前同步IO的不足. 缓冲区Buffer Buffers是一个对象,包含了一些要写入或读出的数据.在面向流的IO模型 ...
- IJ
ylbtech-IJ: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回顶部 10.返回 ...