LeetCode 71. Simplify Path(简化路径)
来源:互联网 发布:手机crm软件 编辑:程序博客网 时间:2024/05/16 16:20
原题网址: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"
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"
.
方法:通过栈来保存当前路径。
public class Solution { /*"./""/""/sa///./aa""/sa////aa""/.""a/b/c/d///""/a/./b/../../c/""/home/../../.."*/ public String simplifyPath(String path) { char[] pa = path.toCharArray(); int pos = 0; for(int i=0; i<pa.length; i++) { if (pa[i] == '/') { if (pos>0 && pa[pos-1] != '/') pa[pos++] = pa[i]; } else if (pa[i] == '.') { if ((((pos==0) || (pos>0 && pa[pos-1] == '/')) && i+2<pa.length && pa[i+1]=='.' && pa[i+2]=='/') || (((pos==0) || (pos>0 && pa[pos-1] == '/')) && i+2==pa.length && pa[i+1]=='.')) { //upper level pos--; while (pos>0 && pa[pos-1] != '/') pos --; if (pos<0) pos = 0; i ++; } else if (((pos==0) || (pos>0 && pa[pos-1] == '/')) && (i+1==pa.length || (i+1<pa.length && pa[i+1] == '/'))) { // same level } else { // normal dir pa[pos++] = pa[i]; } } else { pa[pos++] = pa[i]; } } if (pos>0 && pa[pos-1] == '/') pos --; char[] simplified = new char[pos+1]; simplified[0] = '/'; for(int i=0; i<pos; i++) simplified[i+1] = pa[i]; return new String(simplified); }}
词法和语法分开可以简化:
public class Solution { public String simplifyPath(String path) { String[] paths = path.split("/"); int len = 0; for(int i=0; i<paths.length; i++) { if ("..".equals(paths[i])) { len = Math.max(--len, 0); } else if (!"".equals(paths[i]) && !".".equals(paths[i])) { paths[len++] = paths[i]; } } StringBuilder sb = new StringBuilder("/"); for(int i=0; i<len; i++) { sb.append(paths[i]); if (i<len-1) sb.append("/"); } return sb.toString(); }}
0 0
- LeetCode 71. Simplify Path(简化路径)
- LeetCode-----71. Simplify Path(简化文件路径)
- LeetCode:简化路径(Simplify Path)
- 71. Simplify Path (简化路径)
- Leetcode 71. Simplify Path 简化路径 解题报告
- LeetCode OJ 之 Simplify Path (简化路径)
- Simplify Path 简化文件路径@LeetCode
- [LeetCode]—Simplify Path 简化路径表达式
- 【Leetcode】71 Simplify path 简化路径
- *[Lintcode]Simplify Path简化路径
- LeetCode 71. Simplify Path
- LeetCode --- 71. Simplify Path
- [Leetcode] 71. Simplify Path
- [leetcode] 71.Simplify Path
- [leetcode] 71.Simplify Path
- Leetcode 71. Simplify Path
- LeetCode 71. Simplify Path
- Leetcode 71. Simplify Path
- Java中解析html代码(四)
- 如何在redhat/centos linux服务器中安装git仓库服务
- 自主编写程序实现strcmp函数功能
- 如何解决打开xcode项目是遇到的“cannot be opened because the project file cannot be parsed.”问题?
- Java相关资料网址
- LeetCode 71. Simplify Path(简化路径)
- 判断webview是否彻底加载完成
- java 正则表达式
- 如何让PHP开启CURL支持?
- Quartz任务调度[Spring+Quartz结合]_实现任务的动态添加、修改和删除
- Linux系统相关基础操作
- linux svn中的blame命令
- WebView的用法
- [leetcode]【数组】33. Search in Rotated Sorted Array