71. Simplify Path

主要是用一个stack记录,如果碰到的是“..”,那么如果堆栈不为空,弹出堆栈里面的前一个

如果是“.”,那么什么都不做

 1     public String simplifyPath1(String path) {
 2         if(path == null || path.length() == 0) {
 3             return "";
 4         }
 5         StringBuilder sb = new StringBuilder();
 6         Stack<String> stack = new Stack<String>();
 7         int i = 0;
 8         while(i < path.length()) {
 9             StringBuilder temp = new StringBuilder();
10             int index = i;
11             while(i < path.length() && path.charAt(i) != ‘/‘) {
12                 temp.append(path.charAt(i));
13                 i++;
14             }
15             if(index != i) {
16                 String str = temp.toString();
17                 if(str.equals("..")) {
18                     if(!stack.isEmpty()) {
19                         stack.pop();
20                     }
21                 } else if(!str.equals(".")) {
22                     stack.push(str);
23                 }
24             }
25             i++;
26         }
27         int size = stack.size();
28         for(int j = 0; j < size; j++) {
29             sb.insert(0,stack.pop());
30             sb.insert(0,"/");
31         }
32         return (sb.length() == 0)? "/":sb.toString();
33     }

bug记录

第30,31行是insert,不是append,因为stack是倒序的

时间: 2024-11-01 02:38:17

71. Simplify Path的相关文章

LeetCode开心刷题四十八天——71. Simplify Path

71. Simplify Path Medium 5101348FavoriteShare 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 system, a period . refers to the current directory. Furthermore, a do

71. Simplify Path做题报告

题目链接: 71. Simplify Path 题目大意: 简化路径,如果对于‘.'字符,表明为此目录,对于‘..'字符,表明为回到此目录上一节点(即:删除上一节点),我们现在需要删除多余的'/'字符和'.'字符 做题报告: (1)该题涉及的算法与数据结构与知识点 Java NIO中的Files类,正则表达式,栈 (2)自己的解答思路+代码+分析时间和空间复杂度 栈 class Solution { public String simplifyPath(String path) { String

leetcode 71 Simplify Path

题目连接 https://leetcode.com/problems/simplify-path/ Simplify Path Description Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

leetCode 71.Simplify Path(化简路径) 解题思路和方法

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

[leedcode 71] Simplify Path

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" public class Solution { public String simplifyPath(String path) { /*这道题目是Li

71. Simplify Path (Stack)

Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" class Solution { public: string simplifyPath(string path) { stack<char

71. Simplify Path QuestionEditorial Solution

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" path = "/a/./b/../c/", => "/a/c" path = "/a/./b

leetcode 71 Simplify Path ------ java

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 p

【一天一道LeetCode】#71. Simplify Path

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", =

[LeetCode] 71. 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 th