问题:

来源: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. strtok的使用

    /* strtok函数的使用 */ #include <stdio.h> #include <stdlib.h> #include <string.h> // 函数 ...

  2. safari兼容时间格式 NAN

    问题: safari中不支持2018-08-01这种格式转为时间戳会显示NaN 方案: new Date('2018/08/01').getTime() 将-转化为/ // 正则替换 new Date ...

  3. 京东供应链模式TC转运流程

    TC转运分上门提货和自己送货到网点 上门提货是TC委托第三方货运到商家提货,他们没有装货义务,需要商家自己装货等问题 上门提货简要流程: 采购单创建 商家打单打包出库(自己公司内部建单发货) TC预约 ...

  4. WinServer2008R2远程桌面长时间保持连接

    开始------运行------gpedit.msc 计算机配置------管理模板-----Windows组件-----远程桌面服务------远程桌面会话主机------会话时间限制 修改两项设置 ...

  5. requestAnimationFram

    window.requestAnimationFrame() 告诉浏览器——你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画.该方法需要传入一个回调函数作为参数,该回调函数会 ...

  6. react父子之间传值

    1,父组件向子组件传值 2.子组件向父组件传值 3.没有嵌套关系的组件传值 // 父组件 var MyContainer = React.createClass( { getInitialState: ...

  7. The Preliminary Contest for ICPC Asia Shanghai 2019 J. Stone game

    题目:https://nanti.jisuanke.com/t/41420 思路:当a(a∈S′)为最小值 如果Sum(S′)−a≤Sum(S−S′)成立 那么(∀t∈S′,Sum(S′)−t≤Sum ...

  8. Java 集合基础详细介绍

    一.Java集合框架概述 集合.数组都是对多个数据进行存储操作的结构,简称Java容器.此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt, .jpg, .avi,数据库中).Jav ...

  9. package.json文件

    http://javascript.ruanyifeng.com/nodejs/packagejson.html#toc7(copy) 通常我们使用npm init命令来创建一个npm程序时,会自动生 ...

  10. psdash-为开发、测试人员提供简单的方法,在web界面查看服务器的运行情况(网络,带宽,磁盘,CPU), 同时可以在web界面查看日志

    psdash是linux的系统信息web指示板主要由使用数据psutil——由此得名. github地址:https://github.com/Jahaja/psdash 特性 安装 开始 配置 截图 ...