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

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

注意:题目中已限定是absolute path - contains the root directory and all other subdirectories in which a file or folder is contained.

class Solution {
public:
string simplifyPath(string path) {
stack<string> pathStack;
string splitPath = path;
string curPath;
string ret="";
int pos = ;//absolute path, so path[0]=='/'
while(pos!=string::npos && pos+ < splitPath.length()){
splitPath = splitPath.substr(pos+);
pos = splitPath.find_first_of('/');
if(pos!=){ //ignore second '/'
curPath = splitPath.substr(,pos); if(curPath=="."){//ignore './' }
else if(curPath==".." ){ //ignore '/..'
if(!pathStack.empty()) pathStack.pop();
}
else{ //除了"."以及".."以外的都是文件名,比如"..."
pathStack.push(curPath);
}
}
} if(pathStack.empty()) return "/";
while(!pathStack.empty()){
ret = "/" + pathStack.top() + ret;
pathStack.pop();
}
return ret;
}
};

71. Simplify Path (Stack)的更多相关文章

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

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

  2. 71. Simplify Path(M)

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

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

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

  4. 【LeetCode】71. Simplify Path

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

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

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

  6. [LC] 71. Simplify Path

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

  7. 71. Simplify Path

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

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

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

  9. Leetcode#71 Simplify Path

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

随机推荐

  1. 异常Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://maven.aliyun.com/nexus/content/groups/public was ...

    错误异常:Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://maven ...

  2. lapis 数据库配置

    备注:  目前支持 postgresql .mysql (实际使用大家可以尝试用下tidb.CockroachDB)   1. pg数据库配置 // config.lua local config = ...

  3. Proteus 仿真运算放大器出现 GMIN 问题

    Proteus 仿真运算放大器出现 GMIN 问题 为了仿真一个反相运算放大器,在仿真时出现 GMIN 问题,将 后面的 4.7UF 去掉就可以正常仿真. 初步检查是因为输入频率太低,输入时我用的是 ...

  4. (转)java使用jsp servlet来防止csrf 攻击的实现方法

    背景: 1.csrf知识 CSRF(Cross-site request forgery跨站请求伪造,也被称为“one click attack”或者session riding,通常缩写为CSRF或 ...

  5. (二)Fiddler抓取Firefox、Chrome浏览器上的https协议

    Fiddler抓取Firefox.Chrome浏览器上的https协议 安装Fiddler后默认只抓取http协议,如果是https协议的话,浏览器就会提示"您的链接并不安全". ...

  6. jQuery实现页面监听(某一个值发生改变时触发)

    使用jQuery实现页面中监听   页面中某一个值(如input输入框)发生改变时触发. <html> <head> <title>RunJS</title& ...

  7. cf Double Happiness(判断是否为素数且为4k+1型)

    2790. Double Happiness   time limit per test 3 seconds memory limit per test 128 megabytes input sta ...

  8. HTML5的LocalStorage和sessionStorage的使用

    本文转载自:http://www.cnblogs.com/qiutianlidehanxing-blog/p/5953746.html html5中的Web Storage包括了两种存储方式:sess ...

  9. (转)winform安装项目、安装包的制作、部署

    本文转载自:http://zhan.renren.com/cxymst?gid=3602888498037535727&from=post&checked=true 1,解决方案—添加 ...

  10. Linux:WebServer(Nginx 虚拟主机配置与伪静态实现)

    ps + 查看方式  |  grep  +  服务/端口/软件等:查看状态: 一.基本操作 Nginx 多用于商业系统: 一个端口只能被一个服务使用: Nginx 可以同时监听多个端口,也就是配置时, ...