071 Simplify Path 简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化。
例如,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
边界情况:
你是否考虑了 路径 = "/../" 的情况?
在这种情况下,你需返回"/"。
此外,路径中也可能包含多个斜杠'/',如 "/home//foo/"。
在这种情况下,你可忽略多余的斜杠,返回 "/home/foo"。
详见:https://leetcode.com/problems/simplify-path/description/
Java实现:
class Solution {
public String simplifyPath(String path) {
Stack<String> stk=new Stack<String>();
String[] strs=path.split("/");
for(String str:strs){
if(!stk.isEmpty()&&str.equals("..")){
stk.pop();
}else if(!str.equals(".")&&!str.equals("")&&!str.equals("..")){
stk.push(str);
}
}
List<String> res=new ArrayList(stk);
return "/"+String.join("/",res);
}
}
C++实现:
class Solution {
public:
string simplifyPath(string path) {
stack<string> stack;
int i = 0;
while (i < path.size()) {
// 跳过斜线'/'
while (i < path.size() && '/' == path[i])
{
++i;
}
// 记录路径名
string s = "";
while (i < path.size() && path[i] != '/')
{
s += path[i++];
}
// 如果是".."则需要弹栈,否则入栈
if (".." == s && !stack.empty())
{
stack.pop();
}
else if ("" != s&&s != "."&&s != "..")
{
stack.push(s);
}
}
// 如果栈为空,说明为根目录,只有斜线'/'
if (stack.empty())
{
return "/";
}
// 逐个连接栈里的路径名
string s = "";
while (!stack.empty())
{
s = "/" + stack.top() + s;
stack.pop();
}
return s;
}
};
071 Simplify Path 简化路径的更多相关文章
- lintcode 中等题:Simplify Path 简化路径
题目 简化路径 给定一个文档(Unix-style)的完全路径,请进行路径简化. 样例 "/home/", => "/home" "/a/./b ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- [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 ...
- [LeetCode] 71. Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- Leetcode71. Simplify Path简化路径
给定一个文档 (Unix-style) 的完全路径,请进行路径简化. 例如, path = "/home/", => "/home" path = &qu ...
- Simplify Path(路径简化)
问题: 来源:https://leetcode.com/problems/simplify-path Given an absolute path for a file (Unix-style), s ...
- Java for LeetCode 071 Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...
- LeetCode OJ:Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
随机推荐
- Linux_学习_02_ 重启tomcat与查看tomcat日志
一.重启tomcat服务器 cd /home/ehlhec/tomcat_dingtalk/bin ./shutdown.sh ps -ef|grep java ./startup.sh (1) 进入 ...
- hdu-5818 Joint Stacks(模拟)
题目链接: Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 第十四章-MySQL
1 安装 MySQL常见的版本 GA: 广泛使用的版本 RC: 最接近正式版本 Alpha和Bean: 内测版本和公测版本 有两种安装方式: 安装包和压缩包 1) 安装msi文件 2) 解压zip文件 ...
- 1053 Path of Equal Weight (30)(30 分)
Given a non-empty tree with root R, and with weight W~i~ assigned to each tree node T~i~. The weight ...
- 恋恋风辰 对于redis底层框架的理解(一)
近期学习了redis底层框架,好多东西之前都没听说过,算是大开眼界了. 先梳理下redis正常的通讯流程吧 首先服务器启动都有主函数main,这个main函数就在redis.c里 首先是initser ...
- QT时钟绘制
Demo的效果 资源占用还能接受 运行久一点内存就下去了 下面是Demo的代码 #include "mainwindow.h" #include "ui_mainwind ...
- chromium浏览器开发系列第四篇:如何调试最新chromium
接二连三的事情,时间比较紧张,但是还是没有把这个系列的文章丢掉,因为这也是对自己知识的总结吧.提倡大家多写写,以后再看的时候会有种莫名的小激动. 上周写的是chromium的目录结构,好像大家不太感兴 ...
- ENFP喜欢的职业
外向(E)+直觉(N)+情感(F)+知觉(P). 1. 设计:设计本身很能满足ENFP对工作的各种要求,但是有个附加条件就是,这份工作不能让ENFP长时间的一个人工作,没机会和别人交流,也就是说有一个 ...
- 清除@SessionAttributes 网站实现退出登录
在网站实现登录时,我认识了@SessionAttributes,对我来说是真的好用,@SessionAttributes注解可以使得模型中的数据存储一份到session域中. 这样在页面跳转时可以直接 ...
- VIM显示utf-8乱码
找到vim的根目录下的vimrc文件打开,加入下面三行,保存.重开vim即可. set encoding=utf-8 set langmenu=zh_CN.UTF-8 language message ...