【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 ...
随机推荐
- C puzzles详解【16-20题】
第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...
- Web前端年后跳槽必看的各种面试题
幸运且光荣的被老大安排了一个任务 - “去整理些前端面试题”.年前确实不是招人的好时候,所以我们前端团队经过了超负荷的运转,终于坚持过了春节.春节以后就开始招人啦,这套题考察的目标就是基础基础再基础, ...
- Ninject在mvc中的简单配置
前言 Ninject是一款开源的轻量级的依赖注入插件.从接触ioc以来,一直都是使用这个,感觉用起来还是不错的,配置起来也很方便简单.在mvc中更是基本傻瓜式的配置. 开发前的准备 新建一个mvc3项 ...
- linux安装桌面环境(GNOME)VNC连接Linux
1.安装Gnome桌面 这里是使用的脚本安装.代码如下: 加附件 #!/bin/sh #This script is FREE and written by www.vpsyou.com # i ...
- MessageBox详解
MessageBox.Show();可谓是winform开发中用的次数最多的东东啦.先贴一张msdn的图解 msdn好像没有更新哎,只提供了这几种方法,并且参数名称和最新的有差别,但实际上messag ...
- 学习c语言的第9天
#include <stdio.h> int main() { float sum=0,wage=0; int i=1; int num; printf("+++平均工资统计程序 ...
- android 客户端支付宝 php服务器端编写
生成私钥 输入“genrsa -out rsa_private_key.pem 1024”命令,回车后,在当前 bin 文件目 录中会新增一个 rsa_private_key.pem 文件,其文件为原 ...
- 关于js中this的疑问
学习bootstrap.js源码中被js里边的this绕的有点晕 /* ================================================================ ...
- mysql 1093 错误
1093错误: 要更新某表,同时该表有字段值又来自该表的查询语句. 例如: INSERT INTO m_bulletincategory ( OrganizationKey , CategoryNam ...
- jQuery学习笔记(2)
val() 当鼠标放上去的时候,文本消失,鼠标拿开,文本恢复 效果图: code as below: <html xmlns="http://www.w3.org/1999/xhtml ...