71. Simplify Path(M)
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. 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".
class Solution {
public:
string simplifyPath(string path) {
deque<string> qs;
string result;
int plen = path.size();
string::size_type curindex = , lastindex = ; while (lastindex < plen && (curindex = path.find("/", lastindex)) != string::npos)
{
if(path.find("//", lastindex))
{
qs.push_back(path.substr(lastindex, curindex+-lastindex));
lastindex = curindex+;
}else if (path.find("./", lastindex)) {
lastindex = curindex+;
}else if (path.find(".//", lastindex)) {
lastindex = curindex+;
}else if (path.find("../", lastindex)) {
qs.pop_back(); // go back one step
lastindex = curindex+;
}else if (path.find("..//", lastindex)) {
qs.pop_back();
lastindex = curindex+;
}else {
qs.push_back(path.substr(lastindex, curindex+-lastindex));
lastindex = curindex+;
}
} while (!qs.empty()) {
string tmp = qs.front();
qs.pop_front();
result.append(tmp);
}
if(result.size() != ){
result.resize(result.size()-);
}
return result;
}
};
71. Simplify Path(M)的更多相关文章
- 【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 = " ...
- 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 【一天一道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
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
随机推荐
- JavaEE笔记(十二)
代理的三种配置 beans配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- flask前端与后端之间传递的两种数据格式:json与FormData
json格式 双向! 前端 ==>后端:json格式 后端 ==>前端:json格式 html <!-- html部分 --> <form enctype='applic ...
- fatal error: caffe/proto/caffe.pb.h: No such file or directory
solution: $make clean $make all -j8
- BugkuCTF 文件上传测试
前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...
- jmeter分布式压力测试之添加压力机
前提:多台电脑可以互相ping通 1.jmeter的bin目录下的jmeter.properties配置文件里面remote_hosts添加测试机的 IP:端口号,用英文“,”逗号间隔例如:remot ...
- [咸恩静][Real Love]
歌词来源:http://music.163.com/#/song?id=484056972 作曲 : Andrew 崔/Brian Cho/220 [作曲 : Andrew 崔/Brian Cho/2 ...
- PHP学习 类型 变量 常数 运算符
PHP支持下列8种类型 标量类型 scalar type整数 integer浮点数 float double布尔 boolean字符串 string 特殊类型 special typeNULL资源 r ...
- 【Alpha】第二次Scrum meeting
本次会议内容概括如下: 总结了今天各成员分配任务完成情况,对消耗时间进行统计 评估了各自对团队的贡献(影响)程度 初步讨论了日后需要逐步完成的一些任务并发布在github上 具体内容如下表所示: Me ...
- LeetCode-----算法448.找到所有数组中消失的数字
题目: 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. ...
- 第二个Sprint
能够实现三个数,两个操作符的四则运算.