给定一个文档 (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. jenkins maven tomcat做持续集成

    maven 采用 maven 3.0以上的版本.tomcat 采用 tomcat 7.0 以上的版本 1. tomcat 配置用户账号和权限 tomcat-users.xml <role rol ...

  2. listen 55

    There are also green card qualifiers for some non-citizens who invest in America, and for refugees.难 ...

  3. Stop logging "internal dummy connection" in Apache

    Apache 2.x keeps child processes alive by creating internal connections which appear in the log file ...

  4. POJ1195Mobile phones (从二维树状数组到cdq分治)

    Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...

  5. Mesos的quorum配置引发的问题

    Mesos安装完毕后,发现agent无法和master关联(通过WebUI的agent页面无法看到agent信息),查看日志显示: Elected as the leading master! sta ...

  6. Poj_1068 Parencodings

    S     (((( )( )() ) ) ) P-sequence     4 5 6666,表示第i个右括号的左边有几个左括号. W-sequence    1 1 1456,表示第i个右括号和以 ...

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

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

  8. unreal Script(US)一些注意事项

    转自:http://www.cnblogs.com/dongbo/archive/2012/07/10/2585311.html 1.如果计算量很大,考虑使用native Code来完成. 2.如果代 ...

  9. matlab形态学图像处理之strel函数

    转自:http://blog.sina.com.cn/s/blog_b1cd5d330101pmwi.html strel--structuring element 运用各种形状和大小构造元素,基本语 ...

  10. C++制作电压表电流表仪表盘(vs2008)

    Meter类 Meter.h #if !defined(AFX_METER_H__D5802279_6502_4453_BE21_58604877AD39__INCLUDED_) #define AF ...