【Simplify Path】cpp
题目:
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"
.
代码:
class Solution {
public:
string simplifyPath(string path) {
string result = "";
path.append("/"); // some input path not end with "/"
vector<string> ret; // use vector acting as stack
string tmp_part = "";
ret.push_back(string(,path[]));
for ( size_t i = ; i < path.size(); ++i ){
if ( path[i]=='/' ){
if ( tmp_part==".." ){
if ( ret.size()> )
{
ret.erase(ret.end()-);
ret.erase(ret.end()-);
}
}
else
{
if ( tmp_part!="" && tmp_part!="." )
{
ret.push_back(tmp_part);
ret.push_back("/");
}
}
tmp_part = "";
}
else
{
tmp_part += path[i];
}
}
if ( ret.size()> && ret[ret.size()-]=="/" ) ret.erase(ret.end()-);
for ( size_t i = ; i < ret.size(); ++i ) result += ret[i];
return result;
}
};
tips:
主要思想是stack堆栈的数据结构。
1. 利用"/"来作为间隔判断符号
2. tmp_part为两个"/"之间的字符串部分(需要注意输入的path可能不以"/"结尾,解决办法是人工在path后面补上一个)
3. 如果tmp_part为“..”,则出栈两个元素(前提是栈中元素数目足够)
如果tmp_part不为“.”, 且不为".",且不为"", 则入栈(注意,还要包括后面的“/”)
其他部分靠题目提供的一些corner cases来debug了。
这里用到的一个技巧是vector来代替stack;这样做的好处是返回结果时不用将stack的元素倒序组合。
===============================================
第二次过这道题,大体路子还清晰。有一个地方没有考虑周全:从for退出来时,不能默认最后一个元素直接进栈,需要判断最后一个元素是否合规。
还有,用到stack的,进栈最好检查一下是否符合规定,不符合规定,直接往外弹元素。
class Solution {
public:
string simplifyPath(string path) {
stack<string> sta;
int begin = ;
int len = ;
for ( int i=; i<path.size(); ++i )
{
if ( path[i]!='/' ){
len++;
}
else{
if ( len!= )
{
string tmp = path.substr(begin,len);
if (tmp==".")
{}
else if ( tmp==".." )
{
if (!sta.empty()) sta.pop();
}
else
{
sta.push(path.substr(begin,len));
}
}
begin = i+;
len = ;
}
}
if ( begin<path.size() )
{
if ( len!= )
{
string tmp = path.substr(begin,len);
if (tmp==".")
{}
else if ( tmp==".." )
{
if (!sta.empty()) sta.pop();
}
else
{
sta.push(path.substr(begin,len));
}
}
}
string ret = "";
if (sta.empty()) return "/";
while ( !sta.empty() )
{
ret = "/" + sta.top() + ret;
sta.pop();
}
return ret;
}
};
【Simplify Path】cpp的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- UVA P12101 【Prime Path】
题库 :UVA 题号 :12101 题目 :Prime Path link :https://www.luogu.org/problemnew/show/UVA12101
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
随机推荐
- CentOS Linux下一个tomcat起停,查看日志的shell script
CentOS 的tomcat安装目录:/usr/local/tomcat vi MyTomcatUitl.sh 创建文件chmod u+x MyTomcatUtil.sh 赋执行 ...
- shell下添加计划性任务
*/1 * * * * /bin/sh /tmp/hello.sh ###每1分钟执行以下/tmp下的hello.sh脚本 service crond restart 重启任务计划 * 12 * ...
- Delete PeopleSoft Query From the Database
There could be different reasons why a PeopleSoft developer would like to delete a query from the da ...
- 在Entity Framework 中执行T-sql语句
从Entity Framework 4开始在ObjectContext对象上提供了2个方法可以直接执行SQL语句:ExecuteStoreQuery<T> 和 ExecuteStoreC ...
- [leetcode]_Climbing Stairs
我敢保证这道题是在今早蹲厕所的时候突然冒出的解法.第一次接触DP题,我好伟大啊啊啊~ 题目:一个N阶的梯子,一次能够走1步或者2步,问有多少种走法. 解法:原始DP问题. 思路: 1.if N == ...
- centos下安装php环境
centos下安装php环境 安装apache yum install httpd-devel 启动apache /etc/init.d/httpd start 安装mysql yum install ...
- jQuery在HTML文档加载完毕后自动执行某个事件;
原来onchange=“fucntionname(parms)”: <select name="country" id="selCountries_{$sn}&qu ...
- 用web查看hadoop运行状态
转载--http://www.weixuehao.com/archives/621 我们安装完hadoop,下面我们从视觉上看看hadoop怎么玩的. 我们可以在win7系统上,通过web界面,在浏览 ...
- vim之旅
本人是今年的毕业生, 大学很莫名的选择了一个电子商务专业. 由于专业没有实质性的东西可学,加上对电商不敢兴趣, 于是乎我有了大量的时间在宿舍里折腾电脑. 折腾了几年大三决定转业, 大四在还没找工作之前 ...
- 1092. To Buy or Not to Buy (20)
Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy ...