给定一个文档 (Unix-style) 的完全路径,请进行路径简化。
例如,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
边界情况:
    你是否考虑了 路径 = "/../" 的情况?
    在这种情况下,你需返回"/"。
    此外,路径中也可能包含多个斜杠'/',如 "/home//foo/"。
    在这种情况下,你可忽略多余的斜杠,返回 "/home/foo"。
详见:https://leetcode.com/problems/simplify-path/description/

Java实现:

class Solution {
public String simplifyPath(String path) {
Stack<String> stk=new Stack<String>();
String[] strs=path.split("/");
for(String str:strs){
if(!stk.isEmpty()&&str.equals("..")){
stk.pop();
}else if(!str.equals(".")&&!str.equals("")&&!str.equals("..")){
stk.push(str);
}
}
List<String> res=new ArrayList(stk);
return "/"+String.join("/",res);
}
}

C++实现:

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

071 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. [LeetCode] 71. Simplify Path 简化路径

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

  6. Leetcode71. Simplify Path简化路径

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

  7. Simplify Path(路径简化)

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

  8. Java for LeetCode 071 Simplify Path

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

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

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

随机推荐

  1. latex编译过程-关于嵌入所有字体

    我们的初始目的是想在编译的过程中嵌入所有字体 参考 我们进行了设置,但是不起作用,后发现使用pdflatex编译时是不会调用 ps2pdf的 然后,我们就需要了解编译过程 1. 通常,我们使用texs ...

  2. Nginx中如何限制某个IP同一时间段的访问次数

    如何设置能限制某个IP某一时间段的访问次数是一个让人头疼的问题,特别面对恶意的ddos攻击的时候.其中CC攻击(Challenge Collapsar)是DDOS(分布式拒绝服务)的一种,也是一种常见 ...

  3. eclipse的maven工程Dynamic Web Module 2.3 修改为3.0 解决办法

    1. 创建Maven Web工程 2. 项目只有src/main/resources >Java Build Path导入Tomcat运行环境 3. 删除以图片红框中的文件 4. Propert ...

  4. javaCV入门指南:调用FFmpeg原生API和JavaCV是如何封装了FFmpeg的音视频操作?

    通过"javaCV入门指南:序章 "大家知道了处理音视频流媒体的前置基本知识,基本知识包含了像素格式.编解码格式.封装格式.网络协议以及一些音视频专业名词,专业名词不会赘述,自行搜 ...

  5. 洛谷 1344 [USACO4.4]追查坏牛奶Pollutant Control——最大流

    题目:https://www.luogu.org/problemnew/show/P1344 那个边数的限制,只要把边权乘1001再+1即可.乘1001是因为有1000条边,这样流量小的不会因为边数多 ...

  6. linux 中spfvim安装

    1. 安装 git 1.1 安装依赖的包: curl          curl-devel     zlib-devel         openssl-devel      perl      c ...

  7. 点阵字体显示系列之一:ASCII码字库的显示

    http://blog.csdn.net/subfate/article/details/6444578 起因: 早在阅读tslib源代码时就注意到里面有font_8x8.c和font_8x16.c两 ...

  8. dede问答汉字变星号

    在ask模块里面,question.php中,发现了2行代码 $data['title'] = preg_replace("#{$GLOBALS['cfg_replacestr']}#&qu ...

  9. 在Elasticsearch6.X中如何实现去重

    1.前言 Elasticsearch有没有类似mysql的distinct的去重功能呢? 1)如何去重计数? 类似mysql: select distinct(count(1)) from my_ta ...

  10. OpenCPN介绍及编译

    OpenCPN介绍及编译 OpenCPN是一个航海应用软件系统,采用wxWidgets界面框架,支持OpenGL,可以跨平台运行在Windows , Linux , Mac电脑上. OpenCPN是一 ...