题目:

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".

代码:

  1. class Solution {
  2. public:
  3. string simplifyPath(string path) {
  4. string result = "";
  5. path.append("/"); // some input path not end with "/"
  6. vector<string> ret; // use vector acting as stack
  7. string tmp_part = "";
  8. ret.push_back(string(,path[]));
  9. for ( size_t i = ; i < path.size(); ++i ){
  10. if ( path[i]=='/' ){
  11. if ( tmp_part==".." ){
  12. if ( ret.size()> )
  13. {
  14. ret.erase(ret.end()-);
  15. ret.erase(ret.end()-);
  16. }
  17. }
  18. else
  19. {
  20. if ( tmp_part!="" && tmp_part!="." )
  21. {
  22. ret.push_back(tmp_part);
  23. ret.push_back("/");
  24. }
  25. }
  26. tmp_part = "";
  27. }
  28. else
  29. {
  30. tmp_part += path[i];
  31. }
  32. }
  33. if ( ret.size()> && ret[ret.size()-]=="/" ) ret.erase(ret.end()-);
  34. for ( size_t i = ; i < ret.size(); ++i ) result += ret[i];
  35. return result;
  36. }
  37. };

tips:

主要思想是stack堆栈的数据结构。

1. 利用"/"来作为间隔判断符号

2. tmp_part为两个"/"之间的字符串部分(需要注意输入的path可能不以"/"结尾,解决办法是人工在path后面补上一个)

3. 如果tmp_part为“..”,则出栈两个元素(前提是栈中元素数目足够)

如果tmp_part不为“.”, 且不为".",且不为"", 则入栈(注意,还要包括后面的“/”)

其他部分靠题目提供的一些corner cases来debug了。

这里用到的一个技巧是vector来代替stack;这样做的好处是返回结果时不用将stack的元素倒序组合。

===============================================

第二次过这道题,大体路子还清晰。有一个地方没有考虑周全:从for退出来时,不能默认最后一个元素直接进栈,需要判断最后一个元素是否合规。

还有,用到stack的,进栈最好检查一下是否符合规定,不符合规定,直接往外弹元素。

  1. class Solution {
  2. public:
  3. string simplifyPath(string path) {
  4. stack<string> sta;
  5. int begin = ;
  6. int len = ;
  7. for ( int i=; i<path.size(); ++i )
  8. {
  9. if ( path[i]!='/' ){
  10. len++;
  11. }
  12. else{
  13. if ( len!= )
  14. {
  15. string tmp = path.substr(begin,len);
  16. if (tmp==".")
  17. {}
  18. else if ( tmp==".." )
  19. {
  20. if (!sta.empty()) sta.pop();
  21. }
  22. else
  23. {
  24. sta.push(path.substr(begin,len));
  25. }
  26. }
  27. begin = i+;
  28. len = ;
  29. }
  30. }
  31. if ( begin<path.size() )
  32. {
  33. if ( len!= )
  34. {
  35. string tmp = path.substr(begin,len);
  36. if (tmp==".")
  37. {}
  38. else if ( tmp==".." )
  39. {
  40. if (!sta.empty()) sta.pop();
  41. }
  42. else
  43. {
  44. sta.push(path.substr(begin,len));
  45. }
  46. }
  47. }
  48. string ret = "";
  49. if (sta.empty()) return "/";
  50. while ( !sta.empty() )
  51. {
  52. ret = "/" + sta.top() + ret;
  53. sta.pop();
  54. }
  55. return ret;
  56. }
  57. };

【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. html5 canvas画板

    点击查看演示地址 <!DOCTYPE HTML> <html> <title>HTML5-CanvasDrawDemo</title> <meta ...

  2. 关于Ajax跨域

    本人因工作需求,编写了一个测试页面,在页面填写完信息之后去向一个站点请求数据,然后返回结果!一开始是直接用Ajax在脚本中去访问,没有大碍(因为目标地址是本机上的一个网站),但是当站点去外部的网站时, ...

  3. C#语法基础和面向对象编程

    1.C#语法基础 http://www.cnblogs.com/tonney/archive/2011/03/16/1986456.html 2.C#与面向对象基础 很棒的资源,简明扼要,介绍的非常清 ...

  4. Knockout.Js官网学习(event绑定、submit绑定)

    event绑定 event绑定在DOM元素上添加指定的事件句柄以便元素被触发的时候执行定义的JavaScript 函数.大部分情况下是用在keypress,mouseover和mouseout上. 简 ...

  5. tomcat学习笔记2

    LNMT在网站架构中的实现过程: Client --> http --> Nginx --> reverse_proxy (http) --> tomcat (http con ...

  6. LotusPhp入口文件解析

    LotusPhp也是单入口的框架,可以根据需要开启多个应用实例 例如前台页面可以用index.php作为入口文件,后台可以用admin.php作为入口文件,多个应用实例可以共享应用配置和类库或者根本每 ...

  7. spark概论

    一.概述 1.轻:(1)采用语言简洁的scala编写:(2)利用了hadoop和mesos的基础设施   2.快:spark的内存计算.数据本地性和传输优化.调度优化,使其在迭代机器学习,ad-hoc ...

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

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

  9. 小课堂week15 年终小结

    年终小结 一年的最后,想和大家回顾一下今年讲过的技术和书,用一些问答,一起来提炼一下精华. Spark 为什么需要分布式计算? 计算的增长速度超过了硬件的增长,单一服务器无法负荷.多服务器带来的是复杂 ...

  10. mysql数据库开发规范

    对规范的遵守可用二八原则,不要教条.为满足实际需求 可忽视部分规范. 1.索引规范 *目标 |--利用最小的索引成本找到需要的行记录 *原则 |--做前缀匹配 |--选择区分度高的列做前缀索引列 |- ...