给定一个文档 (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. Linux_服务器_03_xxx is not in the sudoers file.This incident will be reported.的解决方法

    1.切换到root用户下,怎么切换就不用说了吧,不会的自己百度去. 2.添加sudo文件的写权限,命令是:chmod u+w /etc/sudoers 3.编辑sudoers文件vi /etc/sud ...

  2. 改变Ecplise项目窗口字体样式

    Eclipse\plugins\org.eclipse.ui.themes_1.1.1.v20151026-1355\css e4-dark_win.css CTabFolder Tree, CTab ...

  3. linux进程学习-进程描述符,控制块

    从数据结构的角度,进程用task_struct结构来描述,称为“进程描述符 (Process Descriptor)”或者“进程控制块(Process Control Block, PCB)”,其包含 ...

  4. OpenCV——旋转模糊

    参考来源: 学习OpenCV:滤镜系列(5)--径向模糊:缩放&旋转 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #defi ...

  5. CodeForces - 592D: Super M(虚树+树的直径)

    Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ s ...

  6. CodeForces - 311B:Cats Transport (DP+斜率优化)

    Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight r ...

  7. ACM学习历程——UVA10112 Myacm Triangles(计算几何,多边形与点的包含关系)

    Description   Problem B: Myacm Triangles Problem B: Myacm Triangles Source file: triangle.{c, cpp, j ...

  8. C++正确的cin输入

    void test1(void) { int number; cout << ">> pls input a integer number:"; while ...

  9. echo命令的简单用法和实例

    在CentOS 6.8版本下,通过实例的形式,展现选项和参数的灵活运用,可以简明的了解echo的用法. 一.语法:echo [SHORT-OPTION]… [STRING]… :echo [选项]…[ ...

  10. VS13+OPCV2.4.11

    转载:http://blog.csdn.net/a934270082/article/details/50843266?locationNum=3&fps=1 1. 配置系统环境变量:计算机 ...