[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"
- 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".
规律:
1、".."表示跳到上一层目录,删掉它上面挨着的一个路径。
2、"."表示当前目录,直接去掉。
3、如果有多个"/"只保留一个。
4、如果最后有一个"/",去掉不要。
Java:
public class Solution {
public String simplifyPath(String path) {
java.util.LinkedList<String> stack = new java.util.LinkedList<String>();
java.util.Set<String> set = new java.util.HashSet<String>(java.util.Arrays.asList("..", ".", ""));
for (String str : path.split("/")){
if (str.equals("..") && !stack.isEmpty()) stack.pop();
if (!set.contains(str)) stack.push(str);
}
String result = "";
for (String str : stack) result = "/" + str + result;
return result.isEmpty() ? "/" : result;
}
}
Python:
class Solution:
# @param path, a string
# @return a string
def simplifyPath(self, path):
stack, tokens = [], path.split("/")
for token in tokens:
if token == ".." and stack:
stack.pop()
elif token != ".." and token != "." and token:
stack.append(token)
return "/" + "/".join(stack)
C++:
class Solution {
public:
string simplifyPath(string path) {
vector<string> v;
int i = 0;
while (i < path.size()) {
while (path[i] == '/' && i < path.size()) ++i;
if (i == path.size()) break;
int start = i;
while (path[i] != '/' && i < path.size()) ++i;
int end = i - 1;
string s = path.substr(start, end - start + 1);
if (s == "..") {
if (!v.empty()) v.pop_back();
} else if (s != ".") {
v.push_back(s);
}
}
if (v.empty()) return "/";
string res;
for (int i = 0; i < v.size(); ++i) {
res += '/' + v[i];
}
return res;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 71. Simplify Path 简化路径的更多相关文章
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- lintcode 中等题:Simplify Path 简化路径
题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 【LeetCode每天一题】Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the ca ...
- 071 Simplify Path 简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化.例如,path = "/home/", => "/home"path = " ...
- Leetcode71. Simplify Path简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...
- Leetcode#71 Simplify Path
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 71. Simplify Path(M)
71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...
随机推荐
- JQuery系列(2) - 事件处理
JQuery事件绑定 (1)on方法 on方法是jQuery事件绑定的统一接口.后面介绍的事件绑定的那些简便方法,其实都是on方法的简写形式. $('li').on('click', function ...
- SignalR入门二、使用 SignalR 2 实现服务器广播
一.概述 这篇教程通过实现一个股票报价的小程序来讲解如何使用SignalR进行服务器端的推送,服务器会模拟股票价格的波动,并把最新的股票价格推送给所有连接的客户端,最终的运行效果如下图所示. 教程:使 ...
- 学习Microsoft Visio(3)
流程图的规范及技巧 一.流程图绘制基本要求 二.流程图绘制规范要点 在进行流程图的绘制过程中,要有一条明晰的流程主线,从而使得流程图脉络更加清晰. 通常来讲,流程图要以开始任务为起点,完成任务为终点. ...
- Linux端口转发工具rinetd
介绍:Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具.Rinetd是单一过程的服务器,它处理任何数量的连接到在配置文件etc/rinetd中指定的地址 ...
- eclipse 配置python环境 json 插件
windows->install new software add 配置python 环境: name:pydev(可随意写) url:http://pydev.org/updates/ (如果 ...
- 从TEB到PEB再到SEH(一)
什么是TEB? TEB(Thread Environment Block,线程环境块) 线程环境块中存放着进程中所有线程的各种信息 这里我们了解到了TEB即为线程环境块, 进程中每一条线程都对应着的自 ...
- 【批处理】for命令
for 命令 学习:https://www.cnblogs.com/Braveliu/p/5081087.html FOR这条命令基本上都被用来处理文本,但还有其他一些好用的功能! 看看他的基本格式( ...
- 23-ESP8266 SDK开发基础入门篇--编写Android TCP客户端 , 加入消息处理
https://www.cnblogs.com/yangfengwu/p/11203546.html 先做接收消息 然后接着 public class MainActivity extends App ...
- siblings() 方法
siblings([selected]) 简介: 给定一个表示一组DOM元素的jQuery对象,该.siblings()方法允许我们在DOM树中搜索这些元素的兄弟节点,并从匹配的元素构造一 ...
- centos7---mysql5.7主从复制读写分离
1 分别在两台centos 7系统上安装mysql 5.7 具体的安装步骤可以见此链接,https://blog.csdn.net/qq_15092079/article/details/816292 ...