Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
For example,
path="/home/"
, =>"/home"
path="/a/./b/../../c/"
, =>"/c"
click to show corner cases.
Corner Cases:
- Did you consider the case wherepath=
"/../"
?
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"
.
这也属于难题吧,不过是一种特别的难题,或者算是很繁的难题。
但是对于我来说就是,需要搞清楚他到底有多少特殊规定。
比如题目不明显的规矩:
1 如何处理/./--直接忽略
2 如何处理/../--回退一个单词(目录)
3 第一个字符必须是'/'
还有明显的规矩:
4 双//处理
5 结尾单词后面不能接'/'
搞清楚之后就好写程序了,就是把一个一个规矩实现,写入新的字符串中。
这些问题都很细,需要耐心处理,而且每个人的处理方法好像都可以有点不一样,不过最主要的就是思路要清晰,不能乱。
下面是注释详细的程序,不带注释就是一团麻了,注释好,思路还算清晰的。
class Solution {
public:
string simplifyPath(string path)
{
string res;
//规矩1: 第一个必定是'/'
res.push_back('/');
for (int i = 1; i < path.length(); i++)
{
string ts = "";
//查找/ /之间的单词,保存在ts中
while (i<path.length() && path[i] != '/')
{
ts += path[i];
i++;
}
//规矩2:遇到..的时候要回退出上一个单词(目录)
if (ts == "..")
{
if (!res.empty() && res.back() == '/')
res.pop_back();
while (!res.empty() && res.back() != '/')
{
res.pop_back();
}
//注意:规矩1,第一个字符必须是'/'
if (res.empty()) res.push_back('/');
}
else if (ts == "")
{
//双个//的时候,只有在空的时候才加入'/';其他时候不用处理,默认i++就掠过了原字符串中的'/'了
if (res.empty())
res.push_back('/');
}
//规矩3:当遇到.的时候自动跃过,既不入栈,也i++,掠过'/'
else if (ts != ".")
{
res.append(ts);
res.push_back('/');
}
}
//规矩4:有单词的时候结尾不能为'/'
if (!res.empty() && res.back() == '/') res.pop_back();
//空的时候,当做第一个字符,遵循第一个规矩:第一个字符必须是'/'
if (res.empty()) return "/";
return res;
}
};
//2014-2-10 update
string simplifyPath(string path)
{
string rs("/");
for (int i = 1; i < path.length(); i++)
{
string tmp;
for ( ; i < path.length() && path[i] != '/'; i++)
tmp.push_back(path[i]);
if (tmp == "..")
{
if (rs.length() > 1) rs.pop_back();
while (rs.length()>1 && rs.back()!='/') rs.pop_back();
}
else if (!tmp.empty() && tmp != ".") rs.append(tmp+"/");
}
if (rs.length() > 1) rs.pop_back();
return rs;
}
分享到:
相关推荐
主要介绍了LeetCode -- Path Sum III分析及实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
LeetCode — Path Sum III分析及实现方法 题目描述: You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need...
leetcode双人赛71.-Simplify-Path-Leetcode 给定一个字符串路径,它是 Unix 样式文件系统中文件或目录的绝对路径(以斜杠“/”开头),将其转换为简化的规范路径。 在 Unix 风格的文件系统中,句点 '.' 指的是当前...
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
https://leetcode.com/problems/simplify-path/ 题目描述 Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path. In a UNIX-style file ...
大佬的leetcode刷题笔记(c++版本)
vs code LeetCode 插件
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
leetcode中文版
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
leetcode 答案Leetcode-emacs 这是一个 Emacs 插件,可以让编写 leetcode 程序变得更容易和更快。 先决条件 leetcode-cli 该工具基于 ...leetcode-path : 指定保存解决方案代码的路径 leetcode-language : 分
100个leetCode详细解答
LeetCode 刷题汇总1
LeetCode 选讲1
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
leetcode刷题, 直接用leetcode的分类方式.
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
这份文档列出了leetcode几乎所有题目(大约134题)的分类以及难度指示,是刷leetcode的必备良品。现在leetcode总的题目数已经达到150题,所以有部分题目没有包含在这个文档中,但是——足够了。po主刷leetcode的时候...
leetcode高频面试笔试题150+道,亲身总结。
LeetCode面试笔试题