LeetCode 71. Simplify Path

淩亂°似流年 2023-06-04 04:58 104阅读 0赞

题目

字符串问题

  1. class Solution {
  2. public:
  3. string simplifyPath(string path) {
  4. string paths[10005];
  5. int pos=0;
  6. paths[0]="/";
  7. string str="";
  8. for(int i=0;i<path.length();i++)
  9. {
  10. if(i==0&&path[i]=='/')
  11. continue;
  12. if(path[i]=='/')
  13. {
  14. if(str=="")
  15. continue;
  16. if(str=="..")
  17. {
  18. if(pos>0){
  19. pos--;
  20. }
  21. str="";
  22. continue;
  23. }
  24. else if(str==".")
  25. {
  26. str="";
  27. continue;
  28. }
  29. else
  30. {
  31. str+='/';
  32. paths[++pos]=str;
  33. str="";
  34. }
  35. }
  36. else {
  37. str+=path[i];
  38. }
  39. }
  40. if(str=="..")
  41. {
  42. if(pos>0){
  43. pos--;
  44. }
  45. }
  46. else if(str!=""&&str!=".")
  47. {
  48. str+='/';
  49. paths[++pos]=str;
  50. }
  51. if(pos!=0){
  52. paths[pos]=paths[pos].substr(0,paths[pos].length()-1);
  53. }
  54. string ans="";
  55. for(int i=0;i<=pos;i++)
  56. {
  57. ans+=paths[i];
  58. }
  59. return ans;
  60. }
  61. };

转载于:https://www.cnblogs.com/dacc123/p/11584554.html

发表评论

表情:
评论列表 (有 0 条评论,104人围观)

还没有评论,来说两句吧...

相关阅读