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. 笔记:Node.js 的 Buffer 缓冲区

    笔记:Node.js 的 Buffer 缓冲区 node.js 6.0 之前创建的 Buffer 对象使用 new Buffer() 构造函数来创建对象实例,但权限很大,可以获得敏感信息,所以建议使用 ...

  2. python编程规范系列--建议08~18

    本系列来自<编写高质量代码 改善python程序的91个建议>的读书笔记整理.  本章主要内容 建议8:利用assert语句来发现问题 建议9:数据交换值时不推荐使用中间交换变量 建议10 ...

  3. oracle之 sqlplus prelim 参数介绍 ( 处理hang )

    从Oracle10g开始,sqlplus提供了一个参数选项-prelim,用这个参数,在系统已经hang的时候.我们可以连接到SGA而不是数据库,也就是说没有session被创建. 一. 通过以下步骤 ...

  4. The SDK platform-tools version ((21)) is too old to check APIs compiled with API 23

    android studio是个坑爹的工具,每次打开文件头都出现如上错误提示. 解决方法: Update your android sdk platform-tools to the revision ...

  5. git还原某个特定的文件到之前的版本

    场景: 对于某个git控制下的文件进行了修改,但是改的不满意,想退回到改之前的版本.假定该文件为 src/main/main.c 解决方法: 第一步: 在命令行中输入 git log src/main ...

  6. 常用 Git 命令使用教程

    下面整理一下自己在开发过程中经常使用到的 Git 命令.使用 GUI 工具的同学,也可以对照起来看看. Git 配置 1. 在安装完成 Git 后,开始正式使用前,是需要有一些全局设置的,如用户名.邮 ...

  7. 使用CXF开发简单的Web Service-HelloWorld(二)

    上篇博文我们介绍了Web Service的基本概念,了解它的基本概念之后,我们这篇博文介绍一个开源的WebService框架-Apache CXF,并实现一个HelloWorld实例. 一.开始之前 ...

  8. mac系统PHP 7.1.12安装xhprof并使用[View Full Callgraph]小记

    前几天从php7.0.x 升级到了php7.2.0版本, 结果装xhprof没有找到能支持对应版本的xhprof 于是又安装了一个php7.1.2的版本(brew install h) 接着安装xhp ...

  9. 框架(yii和thinkphp)中实例化php内置或者扩展中的对象问题

    将php原生语句实例化SphinxClient对象移植到yii2框架中报错 原生语句中这样写: $s = new SphinxClient(); 框架中应该加入反斜杠,这样写: $s = new \S ...

  10. Memory stream is not expandable

    发现项目有一个地方在做图片缩放剪切的一个操作中.碰到有一些特殊的图片会报 Memory stream is not expandable 的错误 跟踪的时候发现是 由方法 originalStream ...