给定一个文档 (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. dynamic 作为参数传入另一个程序集,获取值

    dynamicOBJ.GetType().GetProperty("key").GetValue(dynamicOBJ, null)

  2. NO1:安装VMLinux虚拟机,安装配置Samba实现Linux与Windows文件共享

    离开技术好些年,仅凭记忆开始学习.同时决定在Linux系统学习C语言. 一.下午安装了VM 8.0,安装RedHat Enterpris Server 6.4虚拟操作系统,都还比较顺利. 二.要实现L ...

  3. 集训Day1

    雅礼集训2017Day1的题 感觉上不可做实际上还挺简单的吧 T1 区间加 区间除法向下取整 查询区间和 区间最小值 大力上线段树,把除法标记推到底,加法标记就是按照线段树的来 先拿30 然后60的数 ...

  4. bzoj 1657 [Usaco2006 Mar]Mooo 奶牛的歌声——单调栈水题

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657 #include<iostream> #include<cstdio ...

  5. [RTOS]--uCOS、FreeRTOS、RTThread、RTX等RTOS的对比之特点

    本篇博客就来细数这几个RTOS的特点.   以下内容均来自官方网站或者官方手册Feature的Google翻译的加了我的一些调整,没有任何主观成分. 1. FreeRTOS   FreeRTOS是专为 ...

  6. ZigBee协议

    转载地址http://www.feibit.com/bbs/viewthread.php?tid=140&extra=page%3D1   WSN/Zigbee开源协议栈 1.    msst ...

  7. JavaScript:bootstrap 模态框的简单应用

    最近用上了bootstrap这个强大的前端框架,有空来总结一下.这里记录下模态框的简单应用. 首先,要在页面中引入相应的js.css文件 <link href="css/bootstr ...

  8. Docker入门(三):容器(Containers)

    这个<Docker入门系列>文档,是根据Docker官网(https://docs.docker.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指 ...

  9. Oracle查看表空间和表空间中的对象

    select * from user_tables;--查询所有用户表 select username,default_tablespace from user_users;--查询当前表空间sele ...

  10. 01_SQlite数据库简介