【LeetCode】71. Simplify Path 解题报告(Python)
【LeetCode】71. Simplify Path 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/
题目地址:https://leetcode.com/problems/simplify-path/description/
题目描述:
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 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”.
题目大意
简化linux路径。
解题方法
看到这种来来回回,增增删删的题,一般都想到用栈。
我们把字符串按照/分割之后就得到了每个文件的目录,然后判断路径是添加还是向上层进行返回。这个题很简单了。
有一个地方犯了小错误,不能写成if dir == ‘..’ and stack: stack.pop()。这样的话如果栈是空的,就把..进栈了。
class Solution(object):
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
stack = list()
dirs = path.split('/')
for dir in dirs:
if not dir or dir == '.':
continue
if dir == '..':
if stack:
stack.pop()
else:
stack.append(dir)
return '/' + '/'.join(stack)
日期
2018 年 6 月 26 日 ———— 早睡早起
【LeetCode】71. Simplify Path 解题报告(Python)的更多相关文章
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Leetcode#71 Simplify Path
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 71. Simplify Path(M)
71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
随机推荐
- SourceTree git 工作流
转载自:https://www.cnblogs.com/tian-xie/p/6264104.html 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 win ...
- Linux—查看内核版本、系统版本、系统位数
一.查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@crowe. ...
- 联盛德 HLK-W806 (六): I2C驱动SSD1306 128x64 OLED液晶屏
目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...
- idea 启动debug的时候throw new ClassNotFoundException(name)
idea 启动debug的时候throw new ClassNotFoundException(name) 启动debug就跳转到此界面 解决办法 这个方法只是忽略了抛异常的点,并没有真正解决问题.后 ...
- 22 SHELL 获取当前路径
常见的一种误区,是使用 pwd 命令,该命令的作用是"print name of current/working directory",这才是此命令的真实含义,当前的工作目录,这里 ...
- Spark(八)【利用广播小表实现join避免Shuffle】
目录 使用场景 核心思路 代码演示 正常join 正常left join 广播:join 广播:left join 不适用场景 使用场景 大表join小表 只能广播小表 普通的join是会走shuff ...
- 【Spring Framework】Spring入门教程(七)Spring 事件
内置事件 Spring中的事件是一个ApplicationEvent类的子类,由实现ApplicationEventPublisherAware接口的类发送,实现ApplicationListener ...
- 【Linux】【Services】【KVM】安装与简单配置
1. 环境: 1.1. OS: Red Hat Enterprise Linux Server release 7.4 (Maipo) 1.2. Kernel: 3.10.0-693.el7.x86_ ...
- Layui:select下拉框回显
一..需求场景分析 基于Thymeleaf模板下的layui下选框回显. 二.获得一个Layui标配的下拉框,我们需要在html中填写的内容如下 <div class="layui-f ...
- Redis-5种基础数据结构
Redis基础数据结构 知识整理源于<Redis深度历险 核心原理与应用实践>这本书 Redis 有的数据结构都以 唯一的 key 字符串作为名称,然后通过这个唯一 key 值来获取相应的 ...