给定一个文档 (Unix-style) 的完全路径,请进行路径简化。

例如,

path = "/home/", => "/home"

path = "/a/./b/../../c/", => "/c"

边界情况:

  • 你是否考虑了 路径 = "/../" 的情况?

在这种情况下,你需返回 "/" 。

  • 此外,路径中也可能包含多个斜杠 '/' ,如 "/home//foo/" 。

在这种情况下,你可忽略多余的斜杠,返回 "/home/foo" 。

这道题的要求是简化一个Unix风格下的文件的绝对路径。

字符串处理,".."是返回上级目录(如果是根目录则不处理),重复连续出现的'/',只按1个处理, 如果路径名是".",则不处理;

class Solution {
public:
string simplifyPath(string path)
{
int len = path.size();
string str = "";
stack<string> q;
for(int i = 0; i < len; i++)
{
if(i == len - 1 && path[i] != '/')
str += path[i];
if(path[i] == '/' && str == "")
continue;
else if(path[i] == '/' || i == len - 1)
{
if(str == "..")
{
if(!q.empty())
q.pop();
}
else if(str == ".")
{ }
else
{
q.push(str);
}
str = "";
}
else
str += path[i];
}
string res = "";
while(!q.empty())
{
res = q.top() + res;
res = "/" + res;
q.pop();
}
if(res == "")
return "/";
return res;
}
};

Leetcode71. Simplify Path简化路径的更多相关文章

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

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

  2. [LintCode] Simplify Path 简化路径

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

  3. [LeetCode] Simplify Path 简化路径

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

  4. 【LeetCode每天一题】Simplify Path(简化路径)

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

  5. 071 Simplify Path 简化路径

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

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

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

  7. Simplify Path(路径简化)

    问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...

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

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

  9. 71. Simplify Path

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

随机推荐

  1. php结合phpStudy实例来熟悉CI框架,用的软件是phpStorm+phpStudy

    1.新建项目名字,我的是放在E盘,叫test,主要是包括application,system,index.php.我的控制器和视图不想放在application中,所以我新建了一个文件夹叫phpTes ...

  2. windows环境下,svn未备份情况下重新恢复

    公司有个同事在未打招呼的情况下把公司服务器进行重新装系统,崩溃啊.SVN之前未备份,还好SVN的库(Repositories)还在,如下图: 恢复办法如下: 由于之前安装的就是VisualSVN-Se ...

  3. vue + element-ui实现简洁的导入导出功能

    1.安装ElementUI模块 cnpm install element-ui -S 2.在main.js中引入 import ElementUI from 'element-ui' import ' ...

  4. laravel框架中使用QueryList插件采集数据

    laravel框架中使用queryList 采集数据 采集数据对我们来说真家常便饭,那么苦苦的写正则采集那么一点点东西,花费了自己大把的时间和精力而且没有一点技术含量,这个时候就是使用我们的好搭档Qu ...

  5. LintCode_13 字符串查找

    题目 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始).如果不存在,则返回 -1. 您在真实的面 ...

  6. Js中的onblur和onfocus事件应用介绍

    html页面中,诸如按钮.文本框等可视元素都具有拥有和失去焦点的事件,本文以文本框获得和失去焦点为例简单讲解onfocus和onblur的应用 html页面中,诸如按钮.文本框等可视元素都具有拥有和失 ...

  7. 提示microsoft incremental linker已停止工作解决方法

    解决方案一:项目->属性->链接器->常规 下面的“启用增量链接”,将“是(/INCREMENTAL)”改为“否(/INCREMENTAL:NO)”.不过这又引入了另外一个警 告:F ...

  8. 比特镇步行(Walk)【虚点+bfs+dfs】

    Online Judge:NOIP2016十连测第一场 T3 Label:虚点,bfs,dfs 题目描述 说明/提示 对于100%数据,\(n<=200000\),\(m<=300000\ ...

  9. JZOJ5894【NOIP2018模拟10.5】同余方程

    题目 Description

  10. windows和ubuntn互传文件

    Windows和linux(ubuntu)互传文件简便快捷的方法 现在很多开发和测试的工作环境都是Linux,但测试后期报告的处理一般都是在Windows下完成的,所以需要把结果拿到Windows下. ...