问题:

来源: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. MyEclipse使用教程:使用DevStyle增强型启动

    [MyEclipse CI 2019.4.0安装包下载] DevStyle不仅仅是一组新的主题,它还包含了一个完全改进的启动体验,拥有更直观的UI,帮助开发人员快速启动IDE.DevStyle作为Ec ...

  2. 移动端 使用 vConsole调试

    前言 用vue 写移动端代码,有个报名页面 就在iOS 9下出现问题,vue的循环渲染都正常,一开始的数据也能取到.证明不是vue的兼容性问题 但是在用户点击按钮发现不能点击进入跳转 工具 推荐使用  ...

  3. CSS自适应布局

    目标效果: 缩小浏览器之后 在<head>最前面引入flexible.js <head> ... <script type="text/javascript&q ...

  4. 计算器work_day05

    day_work_05 ------Python是一个优雅的大姐姐 作业计算器 设计思路 按照运算优先级和正则先算括号内的值,提出来判断符号问题,然后依次计算. 分析题目设计了四个函数,分别为a)去括 ...

  5. codevs 5935 小球 x

    题目描述 Description 许多的小球一个一个的从一棵满二叉树上掉下来组成FBT(Full Binary Tree,满二叉树),每一时间,一个正在下降的球第一个访问的是非叶子节点.然后继续下降时 ...

  6. SSM整合之---环境搭建

    SSM整合---环境搭建 l  查询所有用户的信息,保存用户信息 1.pom.xml配置项目所需的jar包 <dependencies> <dependency> <gr ...

  7. (67)c++后台开发

    还记得自己在学校的时候,一直都比较注重的是:编程语言+数据结构与算法.没错,对于一个在校的计算机专业的学生,这是很重要的方面.但是,这往往不够,或许是因为毕业前一直没有进入企业实习,以至于自己在毕业之 ...

  8. sublime text3 - vue修改data,视图无更新

    ubuntu系统使用sublime text3做vue开发的时候遇到了一个问题,就是修改vue文件并保存后视图页面并不会随之修改,只有重新run dev时修改才会生效,原因没找到 猜想应该是subli ...

  9. jquery 给input text元素赋值,js修改表单的值

    简单粗暴: (第一种) $('#checkUserName').attr("value",sessionUser.name); (第二种) $("#checkUserNa ...

  10. String2LongUtil

    public class String2LongUtil { /** * String类型转换成date类型 * strTime: 要转换的string类型的时间, * formatType: 要转换 ...