[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/", ...
随机推荐
- WPF之MVVM(Step3)——使用Prism(1)
使用WPF-MVVM开发时,自己实现通知接口.DelegateCommand相对来说还是用的较少,我们更多的是使用第三方的MVVM框架,其中微软自身团队提供的就有Prism框架,此框架功能较多,本人现 ...
- POJ 1947 Rebuilding Roads 树形DP
Rebuilding Roads Description The cows have reconstructed Farmer John's farm, with its N barns (1 & ...
- java学习笔记(3):网络编程
基本原理 客户端要发起通信,首先得知道运行服务器程序主机的IP地址,然后由网络的基础结构利用目标地址,将发送的信息传递到正确的主机上.地址可以是数字型(IPv4或者IPv6),也可以是字符串(必须先被 ...
- sql篇,动态合并数据
背景: 为何说是一雪前耻呢,想当年,我还小,我出去面试远洋,远远地看着浩哥在那里坐着,然后下班去吃饭,我和东辉却在那里静静地等待着第二轮的技术面试(结果是没有面上,一个是学历问题),终于一个小个子姐姐 ...
- Jquery禁止/恢复按钮与文本框代码
最近,加入了一个小项目,由浩哥带领我们几个人一起开发一个东西.幸运的是,我和胡志婷分到了一组,她可是一个具有丰富经验的牛人,对我也很好,哈哈. --背景 说点正事,最近,我们在进行项目的时候,提到了一 ...
- for循环嵌套
今天复习了分支语句以及for循环,新内容主要讲解了for循环的嵌套: 外循环控制行,内循环控制列 下面几个实例: 五行五列的矩阵 左下角是直角的三角形: 左上角是直角的三角形: 右上角为直角的三角形: ...
- css学习(1)-- 层叠样式表CSS的基础
一.什么是CSS CSS是Cascading Style Sheets的简写,它除了可以轻松设置网页元素的显示位置和格式处,甚至还能产生滤镜,图像淡化,网页淡入淡出的渐变效果. 一个样式表由样式规则组 ...
- HTTP协议开发应用-HTTP&XML协议栈开发
Netty HTTP+XML协议栈开发 由于HTTP协议的通用性,很多异构系统间的通信交互采用HTTP协议,通过HTTP协议承载业务数据进行消息交互,例如非常流行的HTTP+XML或者RESTful+ ...
- AOP动态代理解析3-增强方法的获取
对于指定bean的增强方法的获取一定是包含两个步骤的: 获取所有的增强 寻找所有增强中使用于bean的增强并应用 那么findCandidateAdvisors与findAdvisorsThatCan ...
- PHP之echo/print
1.PHP中有两个基本的输出方式:echo和print: 2.echo和print的区别: **echo:可以输出一个或多个字符串: **print:只允许输出一个字符串,返回值总为1: 3.echo ...