FTPrep, 71 Simplify Path
来源:互联网 发布:科比场均数据每年 编辑:程序博客网 时间:2024/05/29 17:21
这题优点难度
首先,凡是这种文件夹的题目,也就是说有层次的题目,大都是要用stack这种数据结构来处理,为什么?因为涉及到 晚上走一层 往下走一层等操作。stack可以通过push,pop来模拟了进入一层和出来一层这样的操作。
然后就是怎么 通过一个string,读取层级相关的符号,然后构建出一个目录层次 相对应的 stack
代码如下:
class Solution { public String simplifyPath(String path) { if(path==null || path.length()==0) return "/"; int len=path.length(); StringBuilder result = new StringBuilder(); Stack<String> stack = new Stack<>(); int i = 0; while(i<len){ StringBuilder folder= new StringBuilder(); while(i<len && path.charAt(i)!='/'){ folder.append(path.charAt(i)); i++; } String folderName = folder.toString(); if(folderName.equals("..")){ if(!stack.isEmpty()) stack.pop(); } else if(!folderName.equals(".")&& !folderName.equals("")) stack.push(folderName); i++; } if(!stack.isEmpty()){ String[] folders= stack.toArray(new String[stack.size()]); for(int j=0; j<folders.length; j++) result.append("/"+folders[j]); } if(result.length()==0) return "/"; else return result.toString(); }}// LinkedList.push() 是在头部加元素// https://beginnersbook.com/2014/08/linkedlist-push-and-pop-methods-java/ // https://www.tutorialspoint.com/java/util/arraylist_toarray.htm
阅读全文
0 0
- FTPrep, 71 Simplify Path
- LeetCode 71 Simplify Path
- LeetCode(71) Simplify Path
- [leetcode 71] Simplify Path
- leetcode || 71、Simplify Path
- LeetCode(71) Simplify Path
- 71 Simplify Path
- leetcode 71: Simplify Path
- 【leetcode】【71】Simplify Path
- LeetCode_OJ【71】Simplify Path
- leetcode 71 Simplify Path
- LeetCode 71 Simplify Path
- Leetcode 71 Simplify Path
- leetcode-71 simplify path
- LeetCode #71 Simplify Path
- Leetcode NO.71 Simplify Path
- [leetcode-71]Simplify Path(C)
- LeetCode(71) Simplify Path
- oracle create tablespaces and create user ---cxl
- jenkins 从svn下载源码中途断网问题
- Retrofit请求参数注解字段说明
- 最火房卡欣欣十三水房卡棋牌源码下载
- DAGScheduler核心步骤解读
- FTPrep, 71 Simplify Path
- python enumerate的使用
- hadoop实战随笔_0713
- 关于JSP页面的basepath的作用及格式,举例详解
- NRF24L01+接收不正常的问题(只有开机才能接收到一两条数据)
- 对select下拉框的回显数据的处理
- CommonJS规范
- python安装与配置
- Nginx 安装与配置