[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 = & ...
随机推荐
- 【转】GnuPG使用介绍
一.什么是 GPG 要了解什么是 GPG,就要先了解 PGP. 1991 年,程序员 Phil Zimmermann 为了避开政府监视,开发了加密软件 PGP.这个软件非常好用,迅速流传开来,成了许多 ...
- Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划
Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划 [Problem Description] 给你\( ...
- BLE——协议层次结构
未完待续…… BLE协议 Bluetooth Application Applications GATT-Based Profiles/Services Bluetooth Core (Stack) ...
- drf框架(2)
drf框架 """接口: 接口规范: drf的生命周期: 序列化组件: 三大认证:过滤,删选,排序组件 请求,响应,解析,异常 jwt:json web tooken & ...
- RDD&Dataset&DataFrame
Dataset创建 object DatasetCreation { def main(args: Array[String]): Unit = { val spark = SparkSession ...
- Dubbo源码分析:Invoker
背景 调用对象!在调用过程可以使用Filter接口方法.Inovoker调用过程采用了装饰者设计模式.Filter最后一个ExcpetionFilter对象,这个对象之后就调用服务方法.服务对象是配置 ...
- If...else 条件判断和If else嵌套
If(条件表达式){ 如果条件表达式结果为true,执行该处代码. 如果条件表达式结果为false,执行下边代码. }else{ 如果条件表达式结果为false,执行该处代码. } If(条件表达式) ...
- 通过三层交换机实现不同VLAN间的通信
主机的IP地址以及子网掩码已列出,下面将讲解如何配置利用三层交换机来实现不同VLAN间的相互通信 SW1的命令: en //进入特权模式 conf t //全局模式 vlan 10 // ...
- 用Python操作MySQL(pymysql)
用python来操作MySQL,首先需要安装PyMySQL库(pip install pymysql). 连接MySQL: import pymysql connect=pymysql.connect ...
- 升级pip3的正确姿势--python3 pip3 update
升级pip3的正确姿势为: pip3 install --upgrade pip 而不是 pip3 install --upgrade pip3