问题:

来源:https://leetcode.com/problems/simplify-path

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

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".

思路:

先将路径按照“/”切分,然后遍历切分后的路径,利用栈的思想,遇到“”或者“.”就忽略,遇到“..”就出栈(除非栈中没有元素),遇到其他字符串就入栈,最后将栈中的字符串用“/”拼接起来

 import java.util.Stack;
class Solution {
public String simplifyPath(String path) {
String[] subpaths = path.split("/");
Stack<String> stack = new Stack<String>();
for(String subpath: subpaths) {
if(subpath.equals(".") || subpath.equals("")) {
continue;
} else if(subpath.equals("..")) {
if(!stack.isEmpty()) {
stack.pop();
}
} else {
stack.push(subpath);
}
}
StringBuilder stringBuilder = new StringBuilder();
for(String subpath: stack) {
stringBuilder.append("/" + subpath);
}
if(stringBuilder.length() == 0) {
stringBuilder.append("/");
}
return stringBuilder.toString();
}
}

Simplify Path(路径简化)的更多相关文章

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

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

  2. [Swift]LeetCode71. 简化路径 | Simplify Path

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

  3. [LeetCode] Simplify Path,文件路径简化,用栈来做

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

  4. lintcode 中等题:Simplify Path 简化路径

    题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...

  5. 071 Simplify Path 简化路径

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

  6. Leetcode71. Simplify Path简化路径

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

  7. [LintCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...

  8. leetcode面试准备:Simplify Path

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

  9. Simplify Path leetcode java

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

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

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

随机推荐

  1. vue 解决axios 跨域问题

    闲着没事,假期敲vue 请求数据,总结下vue跨越问题 第一种.服务器服务器不支持跨域请求   1.当跨域无法请求的时候我们可以修改工程下config文件夹下的index.js中的dev:{}部分. ...

  2. python内存相关以及深浅拷贝讲解

    3.9 内存相关 3.9.1 id,查看内存地址 >>> v1 = [11,22,33] >>> v2 = [11,22,33] >>> prin ...

  3. java redirect用法

    重定向到其他链接,不做赘述,上代码. @RequestMapping("forward") public ModelAndView alipayforward(HttpServle ...

  4. [每日一讲] Python系列:列表与元组

    参考文档 https://docs.python.org/zh-cn/3.7/tutorial/introduction.html#lists """ DATA STRU ...

  5. MyEclipse更改项目名web发布名字不改问题

    步骤: 右键项目-->选择propertis-->MyEclipse-->Project Facets-->Web-->右侧更改Web Context-root 如下图:

  6. CF1260F

    题目大意 一棵树,每个节点的权为L[i]~R[i],一棵树的贡献为\(\sum\limits_{h_{i} = h_{j}, 1 \le i < j \le n}{dis(i,j)}\),其中\ ...

  7. 在vue中结合render函数渲染指定的组件到容器中

    1.demo 项目结构: index.html <!DOCTYPE html> <html> <head> <title>标题</title> ...

  8. XML to HTML

    本章讲解如何把 XML 数据显示为 HTML. 在 HTML 中显示 XML 数据 在上一节中,我们讲解了如何通过 JavaScript 来解析 XML 并访问 DOM. 本例遍历一个 XML 文件 ...

  9. 计蒜客 A1607 UVALive 8512 [ACM-ICPC 2017 Asia Xi'an]XOR

    ICPC官网题面假的,要下载PDF,点了提交还找不到结果在哪看(我没找到),用VJ交还直接return 0;也能AC 计蒜客题面 这个好 Time limit 3000 ms OS Linux 题目来 ...

  10. Windows10 + VS2015 环境下对gdal2.0.1进行64bit编译小结

    这是官方给出的编译指导,但是在实践过程中有几点仍然需要特别注意. Tip 1:不要使用默认的"VS开发人员命令提示"工具,使用该工具会遭遇如下的错误: 正在创建库 gdal_i.l ...