Leetcode 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"
.
题目不难,主要考虑一些特殊情况。
对于path = "/a/./b/../../c/"
, => "/c",模拟一下
先按照'/'对字符串进行分割,得到 [a, . , b, .. , .. , c]
首先进入目录a,注意 '.' 代表当前目录 ,".."代表上一个目录
然后到达'.',还是在当前目录,/a
然后到达'b',这为/a/b
然后到达'..',这是回到父目录,则变为/a
然后到达'..',继续回到父目录,则变为/
然后到达'c',则达到子目录,变为/c
class Solution {
public:
vector<string> split(string& path, char ch){
int index = ;
vector<string> res;
while(index < path.length()){
while(index < path.length() && path[index] == '/') index++;
if(index >= path.length()) break;
int start=index, len = ;
while(index < path.length() && path[index]!='/') {index++;len++;}
res.push_back(path.substr(start,len));
}
return res;
} string simplifyPath(string path) {
vector<string> a = split(path,'/');
vector<string> file;
for(int i = ; i < a.size(); ++ i){
if(a[i] == ".." ){
if(!file.empty()) file.pop_back();
}
else if(a[i]!=".") file.push_back(a[i]);
}
string res="";
if(file.empty()) res ="/";
else{
for(int i = ; i < file.size(); ++ i) res+="/"+file[i];
}
return res;
}
};
Leetcode Simplify Path的更多相关文章
- [LeetCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- [leetcode]Simplify Path @ Python
原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-sty ...
- [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. For example,path = "/home/", ...
- leetcode面试准备:Simplify Path
leetcode面试准备:Simplify Path 1 题目 Given an absolute path for a file (Unix-style), simplify it. For exa ...
- 【LeetCode】71. Simplify Path 解题报告(Python)
[LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】71. Simplify Path
Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = " ...
- [LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it. Have you met this question in a real in ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- ecshop 后台分页功能
Ecshop分页规则,分以下几个步骤 1.点击类别,获取第一页获取默认分类列表数据 2.点击“下一页”,采用ajax调取分页内容 实例分析(比如订单列表分页admin/order.php) 1.先写一 ...
- 【转】Java enum的用法详解
用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. p ...
- SQL实现类似于自动刷新数据的功能
有时需要在SQL中,定时刷新某张表,比如说是要定时查询某张表的行数,通常做法就是手动的按F5去执行来刷新数据.但是如果这个定时查询历时较长,10分钟,或半小时,手动的话肯定是要崩溃了.貌似SQL没有像 ...
- oracle创建用户
--首先用管理员的帐户登录(要有修改用户的权限)system,默认数据库orcl. CREATE USER lcs IDENTIFIED BY lcs default tablespace lics_ ...
- poj 1695
用动态规划,dp[a][b][c]表示从位置最大的车在a(注意不是第一辆车),第二的车在b,第三的车在c开始最少需要的时间. 方程:dp[a][b][c]=max{dp[a+1][b][c], ...
- 实现统一用户体验的BaseActivity
对一个规模较大的App开发团队来说,保持统一的代码规范是个好的事情,同时,保持统一的用户体验规范也是个好的事情. 当用户进入一个页面时,一般会有以下交互场景:场景1, 初始化loading,页面从se ...
- python3 黑板客爬虫闯关游戏(一)
这是学习python爬虫练习很好的网站,强烈推荐! 地址http://www.heibanke.com/lesson/crawler_ex00/ 第一关猜数字 很简单,直接给出代码 import ur ...
- JavaScript——基本的瀑布流布局及ajax动态新增数据
本文用纯js代码手写一个瀑布流网页效果,初步实现一个基本的瀑布流布局,以及滚动到底部后模拟ajax数据加载新图片功能. 缺点: 1. 程序不是响应式,不能实时调整页面宽度: 2. 程序中当新增ajax ...
- sharepoint OWA问题解决
请检查DNS 检查域名解析 cmd----nslookup---输入IP或者域名(计算机名)可以看到
- ecplise 常用快捷键
/* * alt+/ * * A:main * main+alt+/ * B:输出语句 * syso+alt+/ * C:提示作用 * * */ /* * 常用快捷键 * 1.格式化:ctrl+shi ...