题目:

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 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的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. 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~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. UVA P12101 【Prime Path】

    题库 :UVA 题号 :12101 题目 :Prime Path link :https://www.luogu.org/problemnew/show/UVA12101

  5. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  6. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  7. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  8. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

随机推荐

  1. Connect to a Windows PC from Ubuntu via Remote Desktop Connection

    http://www.7tutorials.com/connecting-windows-remote-desktop-ubuntu A useful feature of Windows is be ...

  2. 深入了解Javascript模块化编程

    本文译自Ben Cherry的<JavaScript Module Pattern: In-Depth>.虽然个人不太认同js中私有变量存在的必要性,但是本文非常全面地介绍了Javascr ...

  3. Div 不换行、垂直居中等样式

    1. Div内文本过长不换行 1.1 文本不换行 超出部分显示"..." .style1 { float:left; white-space:nowrap; text-overfl ...

  4. js冒泡事件之之之

    console.log("event.target="+event.target); console.log("event.target="+event.tar ...

  5. C#访问配置文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  6. Outlook 2007无法打开链接"由于本机的限制 该操作已被取消"

    编写人:CC阿爸 2014-2-17 近来在日常维护中,经常性的遇到用户在outlook中打开链接,提示[由于本机的限制,该操作已被取消],第一次的在网上搜索到解决办法后, 第二次再处理时,又没能记住 ...

  7. IIS 7.5 配置Asp+Access的几点注意的地方

    环境:window2008 R2 + iis 7.51 把网站程序放在一个www文件夹里面,给这个文件夹添加everyone的用户,赋予全部读写权限,这样安全些.2 选中要配置的网站,点击页面中间“A ...

  8. php 判断table 是否存在 根据返回值继续下一步的操作

    根据sql命令创建数据库或者数据表时候,判断库或者表是否存在比较重要. //要创建的表是否已经存在 function isHaveTable( $dbName,$tableN, $con)  //数据 ...

  9. C#使用SQL存储过程完整流程

    存储过程就是固化在SQL数据库系统内部的SQL语句,这样做的好处是可以提高执行效率.提高数据库的安全性.减少网络流量.接下来就讲解如何在数据库中建立一个存储过程. 打开SQL2055数据库,展开“数据 ...

  10. SQLite中命令行程序(CLP)的使用

    SQLite CLP是使用和管理SQLite数据库最常用的工具.它在所有平台上的操作方式相同.CLP其实是两个程序,它可以运行在Shell模式下以交互的方式执行查询操作,也可以运行在命令行模式下完成各 ...