[LintCode] Simplify Path 简化路径
Given an absolute path for a file (Unix-style), simplify it.
"/home/"
, => "/home"
"/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"
.
LeetCode上的原题,请参见我之前的博客Simplify Path。
解法一:
class Solution {
public:
/**
* @param path the original path
* @return the simplified path
*/
string simplifyPath(string& path) {
int left = , right = , n = path.size();
stack<string> s;
string res = "";
while (right < n) {
while (left < n && path[left] == '/') ++left;
right = left;
while (right < n && path[right] != '/') ++right;
string t = path.substr(left, right - left);
if (t == "..") {
if (!s.empty()) s.pop();
} else if (t != ".") {
if (!t.empty()) s.push(t);
}
left = right;
}
while (!s.empty()) {
res = "/" + s.top() + res; s.pop();
}
return res.empty() ? "/" : res;
}
};
解法二:
class Solution {
public:
/**
* @param path the original path
* @return the simplified path
*/
string simplifyPath(string& path) {
string res, t;
stringstream ss(path);
vector<string> v;
while (getline(ss, t, '/')) {
if (t == "" || t == ".") continue;
if (t == ".." && !v.empty()) v.pop_back();
else if (t != "..") v.push_back(t);
}
for (string s : v) res += "/" + s;
return res.empty() ? "/" : res;
}
};
[LintCode] Simplify Path 简化路径的更多相关文章
- 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 = " ...
- [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 ...
- LeetCode OJ:Simplify Path(简化路径)
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- 71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
随机推荐
- Shell拆分大文件
需求:由于文件过大,不方便进行相关的操作,需要将其拆分成大小小于500000B,即488.28125k的文件.同时,为了保证文件的可读性,行内不可以分割,同时,由于内容是块状可读,按照日期进行分割的, ...
- Intent.putExtra()传递Object对象或者ArrayList<Object> (转)
Intent传递基本类型相信大家都十分熟悉,如何传递Object对象或者ArrayList<Object>对象呢? 可以通过: (1)public Intent putExtra (Str ...
- 回忆一次面试Android研发的问题
有NDK开发JNI程序经验优先 intent intentfileter 阿里云 线程,异步 1.图片缓冲2.解压3.获取搜索记录 4.在安卓开发过程中用到那些框架
- 导出word使用模版
在我们做我们的小组项目的时候,刚开始的时候我们用到的是Mvc+EF,用上了我们的ITOO框架.在最开始的计划,我们要用到瑞郎报表.可是呢,由于工期原因以及技术暂时没有实现,我们不得不想一个比较折中的方 ...
- JAVA Day2
标识符(类名:变量.属性.方法名: ) 组成:类名开头不能是数字,只能有字母数字_$组成. 命名规范: 类名每一个单词首字母大写(HelloWorld大驼峰法则) ...
- 解决Eclipse建Maven项目module无法转换为2.3
Maven项目在Project Facets里面修改Dynamic web module为2.3的时候就会出现Cannot change version of project facet Dynami ...
- 编译报错dereferencing pointer to incomplete type
关于编译报错“dereferencing pointer to incomplete type... 多是没找到结构体的定义,可以在本地复制其定义试试. 参考: http://my.oschina.n ...
- css布局1
body <body> <header id='title'> <h1>Holla</h1> </header> <div id='c ...
- json数据实际应用
JSON序列化输出 var xiaoming = { name: '小明', age: 14, gender: true, height: 1.65, grade: null, 'middle-sch ...
- JS实现选择不同select标签option值的验证
js实现不同select标签option值的验证 功能描述: 选择中文时,匹配中文的正则表达式,选择英文选项是匹配英文的表达式,并且有对应的提示信息. html代码片段: <select id= ...