给定一个文档 (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. PAT甲级——A1071 Speech Patterns

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  2. java基础之DateFormat类

    DateFormat DateFormat类概述 DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间. 是抽象类,所以使用其子类SimpleDateFor ...

  3. Undertow服务器基础分析 - XNIO

    阅读更多 我们从名字上就能看出这是一个NIO思想为基础的IO框架,X是指这个框架可以有多种实现,我们可以从代码库 https://github.com/xnio 中发现一个项目xnio-native, ...

  4. JS倒计时两种种实现方式 很不错

    最近做浏览器界面倒计时,用js就实现,两种方式: 一:设置时长,进行倒计时.比如考试时间等等 代码如下: <html> <head> <meta charset=&quo ...

  5. mongoDB可视化工具RoBo 3T的安装和使用

    第一步下载RoBo3T https://robomongo.org/download 第二步安装 双击安装包安装,修改安装路径,不停下一步,点击安装. 一路next,最后到了这个页面直接点击finis ...

  6. Python 编码转换与中文处理

    python 中的 unicode是让人很困惑.比较难以理解的问题. 这篇文章 写的比较好,utf-8是 unicode的一种实现方式,unicode.gbk.gb2312是编码字符集. py文件中的 ...

  7. 软件-UlitraEdit:UIitraEdit

    ylbtech-软件-UlitraEdit:UIitraEdit UltraEdit 是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII 码,完全可以取代记事本(如果电脑配置足够强大),内 ...

  8. D - Tree and Hamilton Path

    题意 给一棵树,问一个排列,使得按顺序走过这些点的路径最长. N<=100000 解法 为了能让每条边被经过的次数达到上界, 我们首先找出重心, 然后容易得出一种排列方案,使得答案为以重心为根的 ...

  9. WIX、Squarespace、WordPress 三者的优劣分别是什么?

    层出不穷的智能建站,模板建站,源码建站,云建站,仿站,各种建站概念都抛洒于红海之中.到底什么样的网站适合自己,什么样的网站值得我们去消费,什么样的网站能长久,是个非常值得思考的问题. 网站建设技术非常 ...

  10. python-web-webbrower-beautifuSoup

    webbrowser:是 Python 自带的,打开浏览器获取指定页面. requests:从因特网上下载文件和网页. Beautiful Soup:解析 HTML,即网页编写的格式. seleniu ...