LeetCode71 Simplify Path
来源:互联网 发布:java word svg 编辑:程序博客网 时间:2024/05/18 19:21
LeetCode71 Simplify Path
问题描述
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = “/home/”, =>“/home”
path = “/a/./b/../../c/”, => “/c”
简单分析
这道题就是简化路径,看到这个路径我就想到了坑爹的windows系列路径分割符是’\’。这道题明显就是Unix的啦(哈哈)。这道题我的思路是先利用分割符进行分割,然后维护一个list,如果进入下一层路径,那么list添加,如果退到上一层路径,list删除最后一个元素。
代码如下
public String simplifyPath(String path) { StringBuilder stringBuilder = new StringBuilder(); String[] strings = path.split("/"); List<String> list = new LinkedList<>(); for (String str:strings ) { if(str.equals("")||str.equals(".")){ continue; } if(str.equals("..")){ if(!list.isEmpty()){ //如果list不为空,那么移除栈顶元素 list.remove(list.size()-1); } continue; } list.add(str); } //根目录 stringBuilder.append('/'); for (String str: list ) { stringBuilder.append(str+'/'); } if(stringBuilder.length()>1){ stringBuilder.deleteCharAt(stringBuilder.length()-1); } return stringBuilder.toString();}
LeetCode学习笔记持续更新
GitHub地址 https://github.com/yanqinghe/leetcode
CSDN博客地址 http://blog.csdn.net/yanqinghe123/article/category/7176678
阅读全文
0 0
- Simplify Path Leetcode71
- LeetCode71. Simplify Path
- Leetcode71: Simplify Path
- leetcode71. Simplify Path
- LeetCode71 Simplify Path
- LeetCode71 Simplify Path java题解
- LeetCode71——Simplify Path
- 算法设计与分析HW8:LeetCode71.Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- 数据结构实验之链表八:Farey序列
- Java中Split函数的用法技巧
- 【BigHereo 21】---T5-《信息系统开发与管理》---系统分析
- Eclipse各版本分析比较
- Linux下编译Android ffmpeg库(mediacodec 硬件解码)
- LeetCode71 Simplify Path
- 稳定匹配算法python实现
- 运行 Unix网络编程 中的第一个例子 unp学习 实例的运行
- HDU4472 Count (递推)
- 25. Reverse Nodes in k-Group
- PCA的数学原理
- 中兴面试——数据统计分析师(西安)
- dlcf
- Win Server 2008 系统中的IIS下以Fastcgi形式配置Bottle或Flask开发的网站