[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/", ...
随机推荐
- JQuery EasyUI validatebox(验证框)
JQuery EasyUI validatebox(验证框) http://www.easyui.info/archives/602.html
- 在Asp.Net MVC中实现CompareValues标签对Model中的属性进行验证
在Asp.Net MVC中可以用继承ValidationAttribute的方式,自定制实现Model两个中两个属性值的比较验证 具体应用场景为:要对两个属性值的大小进行验证 代码如下所示: /// ...
- NSArray 所有基础点示例
#import <Foundation/Foundation.h> //排序算法,应用于 NSArray *arr=[arrs1 sortedArrayUsingFunction:sort ...
- Arduino101学习笔记(十四)—— Flash库
一.一些API 1.打开文件 SerialFlashFile file; file = SerialFlash.open("filename.bin"); if (file) { ...
- Hbase系统架构
HBase 系统架构 HBase是Apache Hadoop的数据库,能够对大型数据提供随机.实时的读写访问.HBase的目标是存储并处理大型的数据.HBase是一个开源的,分布式的,多版本的,面向列 ...
- .net发邮件【转】
对于.NET而言,从2.0开始,发邮件已经是一件非常easy 的事了.下面我给出一个用C#群发邮件的实例,做了比较详细的注解,希望对有需要的朋友有所help. // 引入命名空间using Syste ...
- 《用delphi开发共享软件》-15.2桌面提示器
打开一个配置文件: 打开一个配置文件 操作TStringGrid Procedure EmptyGrid(Var sg:TStringGrid); Var i:Integer; begin do sg ...
- BSGS[bzoj2242][bzoj3122]
数论题. 操作一:直接快速幂就好了. 操作二:我用了exgcd,shy和lyz都喜欢欧拉函数...QAQ最后这块还写错了. 对于ax+by=gcd(a,b)的形式,我们可以把他们变成y'x+p'y=1 ...
- jQuery操作radiobutton
1.获取某个radio选中的值,有三种方法 $("input:radio:checked").val()(*我最喜欢) ; $("input[type='radio'] ...
- js事件绑定细节说明
javascript绑定事件: 经常用jQuery去写,时间长了对原生态的js事件绑定的知识会慢慢淡化或者遗忘了,必须翻出来再次总结,今天再次把js原生态事件的处理做个总结. 从最初开始,谁刚接触ja ...