[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".
如果仅仅从不断replace输入路径的角度出发,会非常复杂。
如果用一个栈来一次存储各级路径的directory名字,然后重组,会简便一些,这也是文件路径简化类题目的常用思路。
为了末尾可以顺序遍历栈重组path,我们不用传统的stack lib,而用vector来实现栈,这样可以方便顺序遍历。
代码:
class Solution {
public:
string simplifyPath(string path) {
if(path.length() == ) return "";
vector<string> v;
int p = , start = ;
while(p < path.length()){
if(path[p] == '/') ++p;
else{
start = p;
while(p < path.length() && path[p] != '/') ++p;
string temp = path.substr(start, p-start);
if(temp == ".."){
if(!v.empty()) v.pop_back(); //遇到".."就出栈
else{
if(path[] != '/') v.push_back(temp); //如果没东西可以出,就要看path是否以根目录开头了,不是以根目录开头的话,".."要进栈的
}
}else if(temp == "."){} //遇到"."就跳过
else if(temp.length() > ){
v.push_back(temp);
}
}
}
string res = (path[] == '/' ? "/" : ""); //重组path
for(vector<string>::iterator i = v.begin(); i < v.end(); ++i){
res.append(*i);
if(i < v.end()-) res.append("/");
}
return res;
}
};
[LeetCode] Simplify Path,文件路径简化,用栈来做的更多相关文章
- Simplify Path(路径简化)
问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [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] Coin Path 硬币路径
Given an array A (index starts at 1) consisting of N integers: A1, A2, ..., AN and an integer B. The ...
- System.IO.Path文件路径类
Path类的静态属性和方法,此类操作不影响物料文件. 属性 char a = System.IO.Path.VolumeSeparatorChar;//: char b = System.IO.Pat ...
- 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]112. Path Sum路径和(是否有路径)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- python函数学习之装饰器
装饰器 装饰器的本质是一个python函数,它的作用是在不对原函数做任何修改的同时,给函数添加一定的功能.装饰器的返回值也是一个函数对象. 分类: 1.不带参数的装饰器函数: def wrapper( ...
- 统计学习五:3.决策树的学习之CART算法
全文引用自<统计学习方法>(李航) 分类与回归树(classification and regression tree, CART)模型是由Breiman等人于1984年提出的另一类决策树 ...
- DataSet转化为DataTable
. DataTable dt = ds.Tables[]; . DataTable dt = dao.FillTables("GetOptions_DKI_City_HCPName" ...
- CentOS6 安装VNCserver
1.下载vncserver yum install tigervnc tigervnc-server -y 2.配置 vncserver vi /etc/sysconfig/vncserver 在文件 ...
- java — 垃圾回收
1. 垃圾回收的意义 在java中,当没有对象指向原先分配给某个对象的内存的时候,这片内存就变成了垃圾,JVM的一个系统级线程就会自动释放这个内存块,垃圾回收意味着程序不再需要的对象是“无用的信息”, ...
- MVC中验证码的实现(经常用,记录备用)
一.目录 1.多层架构+MVC+EF+AUTOFAC+AUTOMAPPER: 2.MVC中验证码的实现(经常用,记录备用) 3.Ligerui首页的快速搭建 二 正文 Ok,我们的验证码开始,这篇文章 ...
- 【Linux】- vi/vim
所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正 ...
- Android api level对照表
转自:blog.csdn.net/lihenair/article/details/49869299 Platform Version API Level VERSION_CODE Notes And ...
- PHPCMS登录后不是进入会员中心而是转入登录前页最新代码
phpcms比如会员在登录前是停留在下载页面的,但是下载页面是要求会员登录后才能下载,所以会员就有这个登陆过程,但是一般的会员系统是登录进会员中心的,就会有点体验不好 这里教大家修改下 能达到登录后 ...
- IE8 兼容CSS3 使用 PIE.htc
在需要的标签中 div { border:; border-bottom: 10px solid transparent; border-image: url(../images/border-img ...