Simplify Path
来源:互联网 发布:enrique iglesias 知乎 编辑:程序博客网 时间:2024/06/01 12:36
leetcode第71题,要求化简linux风格的路径。
这里首先要明确linux路径的表示形式,一般而言,“.”代表当前目录,所以不用做处理,“..”代表返回上一次目录,因此就需要跳到前面那一层目录下。
很明显,这个题用栈来做会很简单,先用“/”分割字符串,然后凡是遇到“.”的情况,一律越过,不作处理,遇到字符则加入栈,遇到“..”则弹出栈,最后连接一下就可以了。
class Solution(object): def simplifyPath(self, path): """ :type path: str :rtype: str """ paths = [p for p in path.split("/") if p != "." and p != ""] print paths stack = [] for p in paths: if p == "..": if len(stack) != 0: stack.pop() else: stack.append(p) return "/"+"/".join(stack)
0 0
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- Simplify Path
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 在ROOT环境下 基于命令进行VPN pptp 拨号器开发
- 两分钟实现安全完备的登录模块
- android仿淘宝详情页面viewPager滑动到最后一张图片跳转的功能
- DOS 下常用的操作命令
- Simplify Path
- Java学习记录--初识java
- 嵌入式Linux菜鸟成长记(八)
- base.css
- 怎样将成员函数指针强制转换成void*指针?
- java 数组
- iframe的缺点
- 同步通信和异步通信
- java内部类