问题描述:
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"
.
原问题链接:https://leetcode.com/problems/simplify-path/
问题分析
这个问题要求返回一个简化的路径表示。对于给定的路径来说,它一般是一个以/斜杠开始的串。从最开始的斜杠/开始,表示是根节点。然后所有的具体路径这样一层层的往下表示。在这个问题里,主要存在着一些情况需要简化,比如说在某一级的目录里它的字符串是"."或者"..",它们表示当前目录或者当前目录的上一级目录。另外,问题描述中也有说到,对于连续出现两个斜杠/的情况也需要考虑。
所以说,问题的重点就是要解决处理这些情况的问题。对于问题的输入来说,它是一个以斜杠分隔的字符串,对于输出的结果来看就是要将这个表示成最简的样式。根据这一点,首先我们就可以有一个这样的思路。首先将给定的字符串按照斜杠给分割成一个字符串数组。这样在每个分隔里,我们去具体的处理。比如当前碰到的字符串是"."或者空字符,那么我们可以直接忽略。而碰到字符串".."的时候呢,因为它要表示上一级目录,我们需要将它上一级所保存的目录值给丢弃掉。这里还有一个特殊情况,就是如果当前已经到达根节点了呢?这时候直接忽略掉这种情况就可以了。这样,在实现里可以使用一个stack来保存目录每一层处理的结果。最后再将这个结果给保存输出就可以了。
按照这个思路,我们可以很容易得到如下的代码:
public class Solution { public String simplifyPath(String path) { if(path.isEmpty()) return path; String[] list = path.split("/"); Stack<String> stack = new Stack<String>(); for(String s : list) { if(s.isEmpty() || s.equals(".")) continue; else if(s.equals("..")) { if(!stack.empty()) stack.pop(); } else stack.push(s); } if(stack.empty()) return "/"; StringBuilder builder = new StringBuilder(); for(String s : stack) { builder.append("/" + s); } return builder.toString(); } }
相关推荐
正确的姿势,学习的态度来刷 LeetCode:高效的代码、简洁的注释、精炼的总结。
leetcode 非官方顺序leetcode题解,主要代码为Python和C++。 leetcode 第1题: leetcode 第2题: leetcode 第3题: leetcode 第4题: leetcode 第5题: leetcode 第6题: leetcode 第7题: leetcode 第9题: ...
leetcode11 top 1. 位运算 LeetCode191 : 二进制位1的个数 LeetCode338 : 比特位运算 2. 字典树 LeetCode209 : 实现一个Trie结构 LeetCode79 : 单词搜索(判断单词是否出现在给定的网格中) LeetCode212 : 单词搜索II...
LeetCode::laptop:LeetCode解决方案
leetcode 答案 leetCode :keyboard:我的 Leetcode 解题答案
lru缓存leetcode 力码 涵盖了 Geeks for Geeks 和 Leet Code 的各种问题。 LeetCode 1 : 二和 (46_Easy) LeetCode 2 : 两个数字相加 (96_Medium) LeetCode 3 : 无重复字符的最长子串 (214_Medium) LeetCode 4 : 两个...
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 $number 题号代表经典 LeetCode 题目,$number.$number 题号表示 LeetCode ...Path Sum 73
LeetCode 在LeetCode和其他编码平台上解决的问题的集合
leetcode 分类 LeetCode :bouquet::bouquet::bouquet: 介绍 leetcode 题解,Issues 会记录 leetcode 解题之路,并使用 label 进行了分类。 目录 链表
Leetcode:Leetcode提交
LeetCode 101:和你一起你轻松刷题(C++)
:fire: Leetcode :fire: 实践使完美 :party_popper: 开玩笑的单元测试 :sparkles: 简单的代码 :artist_palette: 可读代码 入门指南 git clone https: //github.com/tangweikun/leetcode.git cd leetcode npm ...
idea中leetcode插件Rust 中的 LeetCode 解决方案 怎么跑?...,所有解决方案代码都在leetcode::leetcode::editor::en并重用于leetcode 。 它有一个全局结构Solution ,所有解决方案条目都在其中实现。
leetcode双人赛71.-Simplify-Path-Leetcode 给定一个字符串路径,它是 Unix 样式文件系统中文件或目录的绝对路径(以斜杠“/”开头),将其转换为简化的规范路径。 在 Unix 风格的文件系统中,句点 '.' 指的是当前...
leetcode:leetcode刷题
Leetcode:LeetCode解题代码
LeetCode:LeetCode的代码
LeetCode:LeetCode的注释
leetcode:LeetCode问题
加油站问题leetcode LeetCode LeetCode-JS分类列表: :smiling_face_with_smiling_eyes: :flushed_face: :winking_face: :face_with_tongue: :face_with_open_mouth: :beaming_face_with_smiling_...