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
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
随机推荐
- gradle java 简单项目使用
预备环境 gradle 配置好变量,方便生成项目 1. 环境配置 gradle wrapper 生成项目结构 ├── build.gradle ├── gradle │ └── wrapper │ ├ ...
- http接口测试工具
2016-08-28 19:24:55 以全国天气预报为例 https://www.juhe.cn/docs/api/id/39/aid/132 (一)火狐的HttpRequester 在URL中填 ...
- vs2012加载T4MVC模板
(1)在工程项目上右键点击“管理NuGet程序包”,在线搜索T4MVC模板,选择并安装,安装成功后,项目中会添加T4MVC.tt文件及子文件. (2)如果添加了新的控制器,则右击T4MVC.tt文件点 ...
- CMD中文显示为乱码
中文显示为乱码 临时解决方案: 在 CMD 中运行 chcp 936. 永久解决方案: 打开不正常的 CMD 或命令提示符窗口后,单击窗口左上角的图标,选择弹出的菜单中的“默认值”,打开如下图的对话框 ...
- js实现定时调用的函数setInterval()
setInterval是一个实现定时调用的函数,可按照指定的周期(以毫秒计)来调用函数或计算表达式.setInterval方法会不停地调用函数,直到 clearInterval被调用或窗口被关闭 定义 ...
- AIDL初识
AIDL是一个缩写,全称是Android Interface Definition Language,也就是Android接口定义语言. AIDL的设计是为了实现进程间通信,如同两个进程的桥梁,传输一 ...
- SQL 函数:树结构指定父节点遍历所有的子节点
CREATE function [dbo].[Get_DepChildren] ( @ID int ) , ),PID ), Name )) as begin --declare @ID Int -- ...
- 【转】Hadoop学习路线图
按照这个路线图来学习即可. 1.M. Tim Jones的三篇文章: 用Hadoop进行分布式数据处理第1部分(入门):http://www.ibm.com/developerworks/ ...
- JNI的一个简单实例
本例子使用的操作系统MacOS, 64位JVM. JNI编写的几个步骤如下: 编写Java代码,并注明native方法: public class HelloJni { public native v ...
- Go - 指针简介 与 ++/--运算符以及控制语句
指针 Go 语言中,对于指针有一些特殊约束: 1. 不在支持 “->” 符号,所有的指针使用“.” 来操作指针对象的成员变量 2. 指针的默认值为 “nil” ++ 与 -- 作为语句而非表达式 ...