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. 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)的更多相关文章

  1. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  2. 【LeetCode】71. Simplify Path

    Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...

  3. 71. Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  4. 【一天一道LeetCode】#71. Simplify Path

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 71. Simplify Path (Stack)

    Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...

  6. 71. Simplify Path压缩文件的绝对路径

    [抄题]: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&q ...

  7. [LeetCode] 71. Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  8. [LC] 71. Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...

  9. Leetcode#71 Simplify Path

    原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...

随机推荐

  1. libgdx判断矩形重叠碰撞

    有两种方式. 1. 排除法,排除四种不可能重叠的情况就是了. public static boolean IsOverlap( Rectangle rect1, Rectangle rect2 ){ ...

  2. 使用 vi/vim 时,粘贴进新创建文件或空文件的首行内容丢失的解决方法

    只需要进入插入模式后,回车空一行或几行,再粘贴,再把上面的几个空行back回去,就不会丢失首行的内容了.

  3. idea 开启 tomcat 访问日志记录

    all 为 设置为 查看所有类型的请求 (包括ajax)

  4. C语言和python的区别

    Python可以说是目前最火的语言之一了,人工智能的兴起让Python一夜之间变得家喻户晓,Python号称目前最最简单易学的语言,现在有不少高校开始将Python作为大一新生的入门语言.本萌新也刚开 ...

  5. CTE 递归查询全解

    TSQL脚本能实现递归查询,用户使用共用表表达式 CTE(Common Table Expression),只需要编写少量的代码,就能实现递归查询.本文详细介绍CTE递归调用的特性和使用示例,递归查询 ...

  6. let和const----你所不知道的JavaScript系列(2)

    let 众所周知,在ES6之前,声明变量的关键字就只有var.var 声明变量要么是全局的,要么是函数级的,而无法是块级的. var a=1; console.log(a); console.log( ...

  7. 通用shellcode

    所有 win_32 程序都会加载 ntdll.dll 和 kernel32.dll 这两个最基础的动态链接库.如果想要 在 win_32 平台下定位 kernel32.dll 中的 API 地址,可以 ...

  8. 一种C#泛型方法在lua中表示的设计

    在进行lua方法注册的时候, 大多数解决方案直接否定了泛型方法, 因为在lua侧难以表达出泛型, 以及lua的函数重载问题, 函数重载问题可以通过一些特殊方法解决, 而泛型问题是主要问题, 以Unit ...

  9. 我的小游戏上线海外AppStore完整流程心得

    1,购买一台Mac或者用VMWare 安装Mac OS流程,笔者使用VMWare. 先安装Mac OS 10.13,教程,成功后不要着急安装vmtools, 首先更新系统至最新版,因为真机测试往往需要 ...

  10. webpack 打包

    React自发布以来吸引了越来越多的开发者,React开发和模块管理的主流工具webpack也被大家所熟知.那么webpack有哪些优势,可以成为最主流的React开发工具呢? webpack是什么 ...