【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 ...
随机推荐
- Android 中断线程的处理
我现在对一个用户注册的功能1.用ProgressDialog将当前页面设成不可操作(保留返回键 退出ProgressDialog)2.用一个线程clientThread执行数据的提交和返回 问题:考虑 ...
- .net4.0注册到IIS
IIS和.netfw4.0安装顺序是从前到后,如果不小心颠倒了,无所谓. 打开程序-运行-cmd:输入一下命令重新注册IIS C:\WINDOWS\Microsoft.NET\Framework\v4 ...
- Retrieve Only First x Rows
From time to time you may have the need to grab the top X number of rows returned from a table. For ...
- c# TextReader/TextWriter 的类
TextReader以及TextWriter这两个类,非常有用,很多方法都接受它们作为参数. TextReader有两个子类: StringReader/StringWriter 用于读取字符串: S ...
- 理解 JavaScript Scoping & Hoisting(二)
理解 JavaScript Scoping & Hoisting(二) 转自:http://www.jb51.net/article/75090.htm 这篇文章主要介绍了理解 JavaScr ...
- asp.net mvc 用Redis实现分布式集群共享Session。
1.这两天研究Redis搞分布式session问题,网上找的资料都是用ServiceStack.Redis来实现的,但是在做性能测试的时候发现最新的v4版本有限制每小时候最多请求6000次,因为官网开 ...
- Android消息推送完美方案[转]
转自 Android消息推送完美方案 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来 ...
- 7)Java数据类型
Java中的数据类型分为基本数据类型和引用数据类型: 1)基础数据类型有: boolean, byte.short.char, int.float.long, dou ...
- ios 文件操作
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- 刀哥多线程全局队列gcd-09-global_queue
全局队列 是系统为了方便程序员开发提供的,其工作表现与并发队列一致 全局队列 & 并发队列的区别 全局队列 没有名称 无论 MRC & ARC 都不需要考虑释放 日常开发中,建议使用& ...