Given an absolute path for a file (Unix-style), simplify it.

For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c"

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".

注意以下几点:input"/.", output"/"

input"/..", output"/"

input"/...", output"/..."即“...”、“....”等是有效的文件名

class Solution {
public:
string simplifyPath(string path) {
if(path == "/" || path=="")
return path;
string result(,path[]);
int len = path.size();
if(path[len-]!='/'){
path.push_back('/');
len++;
} int j=,start;
stack<int> sstart;
for(int i = ;i<len;){
if(path[i] != '/' && path[i] != '.'){//if(1) while(i<len && path[i]!='/'){
result.push_back(path[i++]);
j++;
}
int flag = j;
while(flag>= && result[flag]!='/'){
flag--;
}
sstart.push(flag+);
if(i<len- && path[i]=='/'){
result.push_back(path[i++]);
j++;
}else if(i == len- && path[i]=='/'){
return result;
}
}else{
if(path[i]=='/' && result[j]=='/')
i++;
else if(i<len- && path[i]=='.' && path[i+]=='/'){
i=i+;
}else if(i<len- && path[i]=='.' && path[i+]=='.'&& path[i+]=='/'){
i = i+;
if(result.size() == )
continue;
else{
if(result[j]=='/'){
start = sstart.top();
sstart.pop();
result.erase(result.begin()+start,result.end());
j = start-;
}else{ // "/.../""output"/.../"
int flag = j;
while(flag>= && result[flag]!='/'){
flag--;
}
sstart.push(flag+);
result.push_back(path[i-]);
result.push_back(path[i-]);
if(i-<len- && path[i-]=='/'){
result.push_back(path[i-]);
j+=;
}else if(i- == len- && path[i-]=='/'){
return result;
}
}
}
}else{
result.push_back(path[i++]);
j++;
}
}//end if(1)
}//end for
while(result[j]=='/' && result.size()!=){
result = result.substr(,j);
j--;
}
return result;
}//end func
};

[LeetCode] Simplify Path(可以不用看)的更多相关文章

  1. [LeetCode] Simplify Path,文件路径简化,用栈来做

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

  2. [LeetCode] Simplify Path 简化路径

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

  3. [leetcode]Simplify Path @ Python

    原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-sty ...

  4. Leetcode Simplify Path

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

  5. leetcode面试准备:Simplify Path

    leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...

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

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

  7. 【LeetCode】71. Simplify Path

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

  8. [LintCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...

  9. 56. Edit Distance && Simplify Path

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...

随机推荐

  1. LightOJ1031 Easy Game(区间DP)

    我可能真想不到这题是区间DP,不过知道是区间DP想了下就AC了. dp[i][j]表示局面为ai...aj先手能获得与后手得分的最大差值 那么转移到当前状态就是枚举中间的位置,分成两边,其中一边先手全 ...

  2. Float Equal Problem

    Understand limitations of floating point representations.Never check for equality with ==. Instead, ...

  3. Java递归搜索指定文件夹下的匹配文件

    import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Queue; /** ...

  4. MySQL数据库管理常用命令

    参考: http://blog.linuxeye.com/419.html 安装 利用RPM包安装MySQL   设置TCP 3306端口的iptables     root密码管理 设置root用户 ...

  5. json格式转换成Map的应用

    jsp 1.引用json.js(将json格式转换成字符串) 2. var name = document.getElementById("name").value; var re ...

  6. 针对css3特性浏览器兼容 封装less

    //--------------------------------------------------- // LESS Prefixer //--------------------------- ...

  7. 内存调试工具Electric Fence

    源码下载地址 注:官方地址下载不了,可能不再维护了,此是一个老项目 efence中相关环境变量控制: 302 /* 303 * See if the user wants to allow mallo ...

  8. 每天学点GDB 13

    ptrace是gdb实现的基石,本文简要介绍一下ptrace. ptrace linux提供的系统调用ptrace,使得一个进程可以attach到另一个进程并进而完整的控制被attach上的进程. 被 ...

  9. Memcached 笔记与总结(1)Linux(CentOS 6.6) 和 Windows(7)下安装与配置 Memcached (1.4.24)与 Memcached 基础命令

    Memcached 官方网站:http://memcached.org/ 官网对其的描述是: What is Memcached? Free & open source, high-perfo ...

  10. PHP 错误与异常 笔记与总结(10)错误处理器测试

    关联文件:myErrorHandler.php (上一篇) 先测试通知级别的错误的自定义处理: testErrorHandler.php <?php require_once 'myErrorH ...