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.

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
class Solution
{
public:
    string simplifyPath(string path)
    {
        string res;
        string tmp;
        vector<string> vec;
        stringstream ss(path);
        while(getline(ss,tmp,'/'))
        {
            if(tmp == "" or tmp == ".")
                continue;
            if(tmp == ".." and !vec.empty())
            {
                vec.pop_back();
            }
            else if(tmp != "..")
            {
                vec.push_back(tmp);
            }
        }
        for(string str:vec)
        {
            res += "/" + str;
        }
        return res.empty()?"/":res;
    }
};

int main()
{
    string str;
    cin>>str;
    Solution s;
    cout<<s.simplifyPath(str)<<endl;
    ;

}

UNIX简化路径的更多相关文章

  1. lintcode 中等题:Simplify Path 简化路径

    题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...

  2. LeetCode 71.简化路径

    LeetCode 71.简化路径 题目描述: 以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径.在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此 ...

  3. Java实现 LeetCode 71 简化路径

    71. 简化路径 以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径. 在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此外,两个点 (-) 表示将 ...

  4. 在Zookeeper中,znode是一个跟Unix文件系统路径相似的节点,可以往这个节点存储或获取数据

    在Zookeeper中,znode是一个跟Unix文件系统路径相似的节点,可以往这个节点存储或获取数据.如果在创建znode时Flag设置为EPHEMERAL,那么当创建这个znode的节点和Zook ...

  5. LeetCode:简化路径【71】

    LeetCode:简化路径[71] 题解参考天码营:https://www.tianmaying.com/tutorial/LC71 题目描述 给定一个文档 (Unix-style) 的完全路径,请进 ...

  6. LeetCode OJ:Simplify Path(简化路径)

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

  7. 【leetcode-71】 简化路径

    (1 pass) 以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径. 在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此外,两个点 (..) 表示 ...

  8. Leetcode71. Simplify Path简化路径

    给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...

  9. LeetCode——71.简化路径

    以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径. 在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此外,两个点 (..) 表示将目录切换到上一级 ...

随机推荐

  1. python下调用不在环境变量中的firefox

    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary binary = FirefoxBinary(r"D: ...

  2. R读取一个数据框 Dataframe,删去其中的某一列

    可以参考:http://blog.sina.com.cn/s/blog_80572f5d0101anxw.html

  3. C++调用Python脚本中的函数

    1.环境配置 安装完python后,把python的include和lib拷贝到自己的工程目录下 然后在工程中包括进去 2.例子 先写一个python的测试脚本,如下 这个脚本里面定义了两个函数Hel ...

  4. Ansible 小手册系列 十九(常见指令表)

    Play 指令 说明 accelerate 开启加速模式 accelerate_ipv6 是否开启ipv6 accelerate_port 加速模式的端口 always_run   any_error ...

  5. Ansible 小手册系列 四(详解配置文件)

    [root@host-172-20-6-120 ansible]# ansible --version ansible 2.2.0.0 config file = /etc/ansible/ansib ...

  6. Web字体(链接)嵌入

    下面是我最近在学习的两种字体嵌入方法 1.@font-face 使用@font-face可以这样做: @font-face{ font-family:"Garamod Premier Pro ...

  7. map、filter、reduce、lambda

    一.map.filter.reduce map(fuction , iterable) 映射 对可迭代对象中的每一项,使用函数去改变 filter(function, iterable) 过滤 可迭代 ...

  8. JDK的BIO, NIO, AIO

    背景知识点我 1. BIO JDK5之前, JDK的IO模式只有BIO(同步阻塞)问题: 因为阻塞的存在, 需对每个请求开启一个线程. 过多的线程切换影响操作系统性能解决: 使用线程池, 处理不过来的 ...

  9. ZOJ 2971 Give Me the Number (模拟,字符数组的清空+map)

    Give Me the Number Time Limit: 2 Seconds      Memory Limit: 65536 KB Numbers in English are written ...

  10. html5: 幽灵按钮

    html: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...