问题:

来源: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. c语言模块化编程

    C语言模块化编程(封装) 模块即是一个.c 文件和一个.h 文件的结合,头文件(.h)中是对于该模块接口的声明.模块化的实现方法和实质:将一个功能模块的代码单独编写成一个.c文件,然后把该模块的接口函 ...

  2. 【NOIP2016提高A组模拟8.15】Throw

    题目 分析 首先对于一个状态(a,b,c),假定a<=b<=c: 现在考虑一下这个状态,的转移方案: \[1,中间向两边跳(a,b,c)-->(a*2-b,a,c).(a,b,c)- ...

  3. 查看有没有绑这个host

    1.查看有没有绑这个host ping broker.vs.amap.com

  4. 一些 sql 调优的总结

    一.sql 优化方案 1)列类型尽量定义成数值类型,且长度尽可能短,如主键和外键,类型字段等等   2)建立单列索引   3)根据需要建立多列联合索引.当单个列过滤之后还有很多数据,那么索引的效率将会 ...

  5. node.js入门学习(五)--Demo模块化改造

    1.node.js中模块的分类 1)node.js内置模块(核心,原生) 所有内置模块在安装node.js时就已经编译成二进制文件,可以直接加载运行(速度较快),部分内置模块,在node.exe这个进 ...

  6. linux下简易端口扫描器

    #include<iostream> #include<string.h> #include<sys/types.h> #include<sys/socket ...

  7. [luogu]P1026 统计单词个数[DP][字符串]

    [luogu]P1026 统计单词个数 题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1&l ...

  8. Bootstap

    Bootstrap框架 Bootstrap框架 Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序 ...

  9. cvtColor

    E:/OpenCV/opencv/sources/modules/imgproc/src/color.cpp CV_RGB2GRAY:RGB--->GRAY.

  10. 2-gitblit添加项目,项目成员

    创建代码仓库,如下图所示, 然后填个项目的名称和描述即可点击保存. 用户也是,还是相同的位置,点击用户,而不是用户中心,创建用户也是填写用户名称和简称,最后对在旁边有个用户的权限,选择相应的代码仓库即 ...