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. jQuery关于复制(复制隐藏文字+显示文字)跳转,Clipboard

    html: (直接上扎实的数据,框子自己搭建) <script type="text/javascript" src="../../js/jquery.min.js ...

  2. cacti监控linux主机时iptables阻碍了udp161端口造成无法监控解

    由于在添加linux主机时,有防火墙启用,造成无法监控linux主机,现有两种方法解决无法监控linux主机问题:可以通过在监控主机执行该命令: snmpwalk -c public -v 2c 19 ...

  3. jieba分词初学

    昨天,做的那个数据分析报告用到了jieba分词.但是只是借用了别人的部分代码.具体函数代表什么还不太明白.今天去官网研究了下..... jieba官网简介 "结巴"中文分词:做最好 ...

  4. 【备忘录】CentOS服务器mysql忘记root密码恢复

    mysql的root忘记,现无法操作数据库 停止mysql服务service  mysql stop 然后使用如下的参数启动mysql, --skip-grant-tables会跳过mysql的授权 ...

  5. Oracle拆分字符串函数与执行调用

    本函数可以将“目标字符串”以“指定字符串”进行拆分,并通过表结构返回结果.代码如下: ); CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHA ...

  6. [模板]LCA的倍增求法解析

    题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询问的个数和树根结点的序号. 接下来N-1行每 ...

  7. GOF23设计模式之外观模式(facade)

    一.外观模式概述 外观模式也称为门面模式. 核心:为了系统提供统一的入口,封装子系统的复杂性,便于客户端调用. 二.外观模式场景导入与示例代码 场景:要想自己去注册一个公司,首先去工商局检测命名是否合 ...

  8. Java-Runoob-高级教程:Java 发送邮件

    ylbtech-Java-Runoob-高级教程:Java 发送邮件 1.返回顶部 1. Java 发送邮件 使用Java应用程序发送 E-mail 十分简单,但是首先你应该在你的机器上安装 Java ...

  9. Java-Runoob-面向对象:Java 抽象类

    ylbtech-Java-Runoob-面向对象:Java 抽象类 1.返回顶部 1. Java 抽象类 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的 ...

  10. java之IO整理(上)

    /*//创建一个新文件 public static void main(String[] args) { File file=new File("D:\\hello.txt"); ...