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
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
随机推荐
- 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp
QSC and Master Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- 转:db2 iptables相关用法(1)
如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...
- html5的改进与沿革
HTML5提供了一些新的元素和属性,例如<nav>(网站导航块)和<footer>.这种标签将有利于搜索引擎的索引整理,同时更好的帮助小屏幕装置和视障人士使用,除此之外,还为其 ...
- 机器翻译 2010年NOIP全国联赛提高组
题目描述 Description 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义 来替换.对于每个英 ...
- kuangbin_ShortPath F (POJ 3259)
判环模板题 有了上一题的经验过得很轻松 除了因为spfa还不是很熟打错了两个字母 然后debug了一小会 #include <iostream> #include <string&g ...
- Useful SQL Server Article
http://blogs.technet.com/b/topsupportsolutions/archive/2013/11/06/top-support-solutions-for-microsof ...
- Python-requests之POST Data的json问题
代码如下: import json import requests r = requests.post(url, data = {"a": json.dumps({"b& ...
- Python内部类型
Python使用对象模型来存储数据 . 身份:每个对象都有一个唯一的身份标识自己,任何对象的身份否可以使用内建函数id()来得到.这个值可以被认为是该对象的内存地址 . 类型:对象的类型决定了该对 ...
- Python_Day6_反射_正则表达式之篇
一.反射 定义:利用字符串形式去对象(模块)中操作(寻找/检查/删除/设置)成员 #getattr:获取模块中属性 #hasattr:检查模块中是否存在某个成员(函数) #delattr:删除模块中成 ...
- 09-Java 工程结构管理
(一)Java 工程结构管理 1.什么是Build Path: -- 一般包括:JRE运行时库 第三方功能扩展库(*.jar 格式文件) 其他的工程 其他的源代码或Class 文件 为什么使用~ :通 ...