题目:

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. [原]sdut2624 Contest Print Server (大水+大坑)山东省第四届ACM省赛

    本文出自:http://blog.csdn.net/svitter 原题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&am ...

  2. minicom/kermit捕捉日志

    1.minicom捕捉日志 ctrl-A Z 命令窗口中有 Capture on/off......L   2.kermit捕捉日志 ctrl-\ C进入kermit命令行模式 log session ...

  3. dedecms后台上传图片附件返回302的问题

    在网上查了资料,验证可行后,在这儿做个备份! 自己解决了,貌似是swfupload在linux环境下session丢失的引起的 解决办法是:在include/userlogin.class.php文件 ...

  4. Conditional Counting In SQL

      If you ever want to conditionally count the number of times a particular condition occurs in SQL, ...

  5. 一些peoplecode小技巧平【二】

    1. Set component changed page field property: For understanding this open a page in application desi ...

  6. datagridview添加复选框全选和取消

    全选 private void All_selected_Click(object sender, EventArgs e) { ; i < this.DataGridViewProduct.R ...

  7. c语言学习的第五天

    #include<stdio.h> #include<stdbool.h> int main() { _Bool num=1; if (num==true); { printf ...

  8. MongoDB的主要特性概述

    一.文档数据模型 文档是一组属性名和属性的集合.相较于关系数据库复杂的规范化,面向文档的数据模型很容易以聚合的形式来表示数据.文档采用无Schema的形式,这种做法带来了一定的优势:首先,由应用程序, ...

  9. ubuntu下svn使用指南

    ubuntu下安装subversion客户端: sudo apt-get install subversion subversion-tools 详细请看 http://www.subversion. ...

  10. SQL Server 基础之《学生表-教师表-课程表-选课表》

    一.数据库表结构及数据 建表 CREATE TABLE Student ( S# INT, Sname ), Sage INT, Ssex ) ) CREATE TABLE Course ( C# I ...