新博文地址:[leetcode]Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
click to show corner cases.
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".
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
click to show corner cases.
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".
首先,要搞懂题目的意思= =//,由于完全木有接触过unix,所以压根就不明白那些/.和/..是啥东西。好吧,原来是这样的
/.表示当前目录,/..表示上级目录,/表示根目录
/a/./b/../../c/为例,
/a表示进入a目录,/.表示当前目录,即还是a目录,因此可以无视
/b表示进入b目录,当前的目录为/a/b
/..表示返回上级目录,即回到了/a
同理,再返回一次,就回到的根目录/
然后进入c目录,最后的目录结构为:/c
tip中给了两点,根目录的上级目录还是根目录,斜线/可能有蛋疼的多条,比如//a、///a其实与/a的效果是一样的。
我的思路比较单纯:
public String simplifyPath(String path) { if(path == null || path.length() == 0){ return null; } while(!path.trim().replaceAll("//", "/").equals(path)){//如果有多条斜线"/",全部替换成单条"/" path = path.trim().replaceAll("//", "/"); } if(path.charAt(path.length() - 1) != '/'){//如果目录最后不是以"/"结尾的,人工的添加上 path += "/"; } StringBuilder simplifyPath = new StringBuilder(); Stack<String> stack = new Stack<String>(); int[] slash = new int[path.length()];//存储每个"/"在path中的下标 int slashIndex = 0; for(int i = 0; i < path.length();i++){ if(path.charAt(i) == '/'){ slash[slashIndex++] = i; } } for(int i = 1; i < slashIndex;i++){ String tem = path.substring(slash[i-1] + 1, slash[i]);//每两个"/"之间的字符串即是一级目录 if(!".".equals(tem) && !"..".equals(tem)){//正常的目录,则压栈 stack.push(tem); }else if("..".equals(tem)){//遇到返回,则弹栈 if(!stack.isEmpty()){//if stack is empty ,do nothing stack.pop(); } } } while(!stack.isEmpty()){ String tem = stack.pop(); simplifyPath.insert(0, "/"+ tem); } return simplifyPath.toString().isEmpty() ? "/" :simplifyPath.toString(); }
相关推荐
c c语言_leetcode题解之0071_simplify_path.zip
javascript js_leetcode题解之71-simplify-path.js
leetcode双人赛71.-Simplify-Path-Leetcode 给定一个字符串路径,它是 Unix 样式文件系统中文件或目录的绝对路径(以斜杠“/”开头),将其转换为简化的规范路径。 在 Unix 风格的文件系统中,句点 '.' 指的是当前...
https://leetcode.com/problems/simplify-path/ 题目描述 Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path. In a UNIX-style file ...
# [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...
- Simplify Path: 给定一个表示文件系统的路径,请实现一个方法,以简化这个路径。 - Edit Distance: 给定两个单词word1和word2,计算将word1转换为word2所使用的最少操作数。 - Set Matrix Zeroes: 给定一个m×n的...
leetcode中国 我自己的leetcode刷题记录 ###[20150920] Valid Palindrome Implement strStr() String to Integer ...Simplify Path,字符串处理,stack Length of Last Word,字符串处理.细节题 Rever
LeetCode第71题的全称是“Simplify Path”,该题目要求我们编写一个程序,将输入的复杂路径字符串简化为最基础的目录序列。例如,输入的路径可能是"/a/./b/../../c/",输出应该是"/c"。这个问题主要考察的是对字符串...
在本压缩包中,主题聚焦于C++编程基础与LeetCode算法题目的解法,特别是针对LeetCode第71题“简化路径”(Simplify Path)的探讨。这道题目旨在检验开发者的路径解析能力,涉及到字符串处理和栈的数据结构应用。让...