[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 ...
随机推荐
- 简单构建基于RDF和SPARQL的KBQA(知识图谱问答系统)
本文主要通过python实例讲解基于RDF和SPARQL的KBQA系统的构建.该项目可在python2和python3上运行通过. 注:KBQA即是我们通常所说的基于知识图谱的问答系统.这里简单构建的 ...
- URAL 1664 Pipeline Transportation(平面图最大流)
Description An oligarch Vovan, as many other oligarchs, transports oil from West Cuckooland to East ...
- POJ 3487 The Stable Marriage Problem(稳定婚姻问题 模版题)
Description The stable marriage problem consists of matching members of two different sets according ...
- 最小生成树(Kruskal和Prim算法)
关于图的几个概念定义: 关于图的几个概念定义: 连通图:在无向图中,若任意两个顶点vi与vj都有路径相通,则称该无向图为连通图. 强连通图:在有向图中,若任意两个顶点vi与vj都有路 ...
- Coursera-Note: Internet History, Technology and Secure (1st week to 9th week)
目录 Coursera-Note: Internet History, Technology and Secure 第一周 第二周 数据交换: Packet switching技术: 第三周 创造ht ...
- HttpServletRequest和HttpServletResponse实例
先看一下web.xml文件配置: <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...
- .net控制台程序Program args参数解析
一直很有疑问在控制台程序的Main函数中为什么会有个string[] args的参数,又没有什么用. static void Main(string[] args) { } 这几天需要将一个控制台程序 ...
- Linux内核策略介绍学习笔记
主要内容 硬件 策略 CPU 进程调度.系统调用.中断 内存 内存管理 外存 文件IO 网络 协议栈 其他 时间管理 进程调度 内核的运行时间 系统启动.中断发生.系统调用以及内核线程. 进程和线程的 ...
- OSG学习:矩阵变换节点示例
#include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...
- idea超炫的自定义模板
idea超炫的自定义模板 idea 有些快捷键 sout -> System.out.println() ,输入sout,idea能自动补全代码, 这种约定的快捷方式大大提高了效率, 而id ...