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.

Corner Cases:

  • Did you consider the case where path = "/../"?
    In this case, you should return "/".
  • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
    In this case, you should ignore redundant slashes and return "/home/foo".

被这一串的文件路径名给唬住了,还是缺乏分析问题的能力啊,path = "/a/./b/../../c/", => "/c"其实就是当前目录和上层目录相互抵消啊,从左往右依次看过去就行了,然后就是以“/”为分割符分割这个字符串了.

以下转自:http://blog.csdn.net/makuiyu/article/details/44497901

字符串处理,由于".."是返回上级目录(如果是根目录则不处理),因此可以考虑用栈记录路径名,以便于处理。需要注意几个细节:

  1. 重复连续出现的'/',只按1个处理,即跳过重复连续出现的'/';
  2. 如果路径名是".",则不处理;
  3. 如果路径名是"..",则需要弹栈,如果栈为空,则不做处理;
  4. 如果路径名为其他字符串,入栈。

最后,再逐个取出栈中元素(即已保存的路径名),用'/'分隔并连接起来,不过要注意顺序呦。

时间复杂度:O(n)

空间复杂度:O(n)

class Solution
{
public:
string simplifyPath(string path)
{
stack<string> ss; // 记录路径名
for(int i = ; i < path.size(); )
{
// 跳过斜线'/'
while(i < path.size() && '/' == path[i])
++ i;
// 记录路径名
string s = "";
while(i < path.size() && path[i] != '/')
s += path[i ++];
// 如果是".."则需要弹栈,否则入栈
if(".." == s && !ss.empty())
ss.pop();
else if(s != "" && s != "." && s != "..")
ss.push(s);
}
// 如果栈为空,说明为根目录,只有斜线'/'
if(ss.empty())
return "/";
// 逐个连接栈里的路径名
string s = "";
while(!ss.empty())
{
s = "/" + ss.top() + s;
ss.pop();
}
return s;
}
};

Simplify Path——简单经典的预处理的更多相关文章

  1. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  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. 56. Edit Distance && Simplify Path

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...

  4. leetcode面试准备:Simplify Path

    leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...

  5. FOR XML PATH 简单介绍

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  6. 71. Simplify Path(M)

    71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...

  7. 【LeetCode】71. Simplify Path

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

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

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

  9. [LeetCode] Simplify Path 简化路径

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

随机推荐

  1. BZOJ2434: [NOI2011]阿狸的打字机(AC自动机+dfs序+树状数组)

    [NOI2011]阿狸的打字机 题目链接:https://www.luogu.org/problemnew/show/P2414 题目背景 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机. ...

  2. bzoj 1122 [POI2008]账本BBB 模拟贪心,单调队列

    [POI2008]账本BBB Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 524  Solved: 251[Submit][Status][Disc ...

  3. ES6数组的扩展运算符

    一.基本使用 ES6中函数可以使用 rest参数 接收函数的多余参数,组成一个数组,放在形参的最后面. let fn = (a, ...value) => { console.log(a); c ...

  4. 南阳ACM 题目275:队花的烦恼一 Java版

    队花的烦恼一 时间限制:3000 ms  |  内存限制:65535 KB 难度:1 描述 ACM队的队花C小+经常抱怨:"C语言中的格式输出中有十六.十.八进制输出,然而却没有二进制输出, ...

  5. POJ 3087 Shuffle'm Up DFS

    link:http://poj.org/problem?id=3087 题意:给你两串字串(必定偶数长),按照扑克牌那样的洗法(每次从S2堆底中拿第一张,再从S1堆底拿一张放在上面),洗好后的一堆可以 ...

  6. 外观模式(Facde)【设计模式】

    定义:为子系统中的一组接口提供一个一致的界面,Fcade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. “外观模式(Facade pattern),是软件工程中常用的一种软件设计模式,它 ...

  7. 从零开始开发一款app,所想到的

    我在知乎上看到这个问题http://www.zhihu.com/question/27645587.我在阅读了各位大牛的答案后,再加上自己的思考,就有了这篇文章的内容.     从零开始开发一款app ...

  8. 【BZOJ】3329: Xorequ

    [题意]给定方程x^3x=2x,求<=x和<=2^x的满足方程的正整数个数. [算法]数位DP,矩阵快速幂 [题解]异或相当于不进位加法. 移项得,x^2x=3x,又因为x+2x=3x,所 ...

  9. response.getWriter().write()和 response.getWriter().print()的区别

    异步上传图片的代码.发现里面用了response.getWriter().print(),故联想到response.getWriter().writer(),经过一番api的查找与实操,总结如下: r ...

  10. Override 和 Overload 的含义和区别

    Override 1.方法重写.覆盖: 2.重写是父类与子类之间多态性的一种表现: 3.方法名,参数,返回值相同: 4.存在于子类和父类之间: 5.修饰为final的方法,不能被重写: Overloa ...