71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
click to show corner cases.
- 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".
==================
简化路径,
注意:1,能够自动优化路径例如,/../可以变为/
2,能够将 多了斜杠/// 简化为一个/
=================
思路:
这里需要一个字符串划分函数,c++中是没有提供字符串划分函数的.
可以参考[ http://www.cnblogs.com/li-daphne/p/5524752.html ]分析过程.
主要是利用了string类中的find,substr函数
----
对输入字符串做slipt之后,所有的路径名字包括[.]和[..]都会存入一个vector中,
此后,我们再利用栈来剔除[.]和[..]路径,
最后一次对栈中的数据进行处理就好了.
============
代码如下:
void myslipt(string &s,vector<string> &re,string &c){
std::string::size_type pos1,pos2;
pos2 = s.find(c);///find
pos1 = ;
while(std::string::npos != pos2){
string t = s.substr(pos1,pos2-pos1);///[p1,p2)
if(!t.empty()){
re.push_back(t);
}
pos1 = pos2+c.size();
pos2 = s.find(c,pos1);
}
if(pos1!=s.length()){
re.push_back(s.substr(pos1));
}
}
string simplifyPath(string path) {
vector<string> re;
string c = "/";
myslipt(path,re,c);
stack<string> st;
for(size_t i = ;i<re.size();i++){
if(re[i]==".") continue;
else if(re[i]==".."){
if(st.empty()){
continue;
}else{
st.pop();
}///if-else
}else{
st.push(re[i]);
}
}
string result;
while(!st.empty()){
result.insert(,st.top());
result.insert(,"/");
st.pop();
}
return result;
}
71. Simplify Path的更多相关文章
- 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 解题报告(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 = " ...
- 【一天一道LeetCode】#71. Simplify Path
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 71. Simplify Path (Stack)
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 简化路径
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 ...
- Leetcode#71 Simplify Path
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
随机推荐
- dennis gabor 从傅里叶(Fourier)变换到伽柏(Gabor)变换再到小波(Wavelet)变换(转载)
dennis gabor 题目:从傅里叶(Fourier)变换到伽柏(Gabor)变换再到小波(Wavelet)变换 本文是边学习边总结和摘抄各参考文献内容而成的,是一篇综述性入门文档,重点在于梳理傅 ...
- C函数及指针学习1
1 大段程序注释的方法 #if 0#endif 2三字母词 以两个问号 开始的都要注意 3 字面值(常量) 在整型号字面值后加 字符L (long),U(unsigned)说明字符常量 为长整型 或( ...
- SQL Server用户自定义函数
用户自定义函数不能用于执行一系列改变数据库状态的操作,但它可以像系统 函数一样在查询或存储过程等的程序段中使用,也可以像存储过程一样通过EXECUTE 命令来执行.在 SQL Server 中根据函数 ...
- (转)ubuntu 14.04下安装hadoop2.6(伪分布式)
安装: http://pingax.com/install-hadoop2-6-0-on-ubuntu/ 写的非常好(国外人比较实在,他说的单机实际上是伪分布式...). 另外要注意的是,如果对新 ...
- 4-1 yum源文件
1.Yum源文件 <1>在Linux中,有这样一个目录 /etc/yum.repos.d/,里面有默认4个yum源文件, 其中Base是基本yum源文件,它是默认生效的 其他的几个默认都是 ...
- apache开启url rewrite模块
在把服务器数据转移到本地服务器之后,本地打开首页出现排版紊乱等问题,经过大神指点说是url rewrite的问题. 本篇文章主要写怎样开启apache的url rewrite功能. 打开Apache2 ...
- php文件遍历
<?php $dirname="shangchuan/uploads"; echo $dirname."共计大小为:".toSize(dirsize($d ...
- Meven笔记
技术交流群:233513714 1.Meven环境搭建 http://www.cnblogs.com/quanyongan/archive/2013/04/17/3025971.html 2.Ecli ...
- Linux内核源代码的结构(转)
源代码所有在目录:/usr/src/linux (大部分linux发行版本中) init 内核初始化代码 kernel 内核核心部分:进程.定时.程序执行.信号.模块... mm 内存处理 a ...
- Linux磁盘空间被未知资源耗尽【转】
Linux磁盘空间被未知资源耗尽 在linux中,当我们使用rm在linux上删除了大文件,但是如果有进程打开了这个大文件,却没有关闭这个文件的句柄,那么linux内核还是不会释放这个文件的磁盘空间, ...