Leetcode 之Simplify Path(36)

主要看//之间的内容:如果是仍是/,或者是.,则忽略;如果是..,则弹出;否则压入堆栈。最后根据堆栈的内容进行输出。

string simplifyPath(string const& path)
      {
          vector<string> dirs;
          for (auto i = path.begin(); i != path.end();)
          {
              i++;
              auto j = find(i, path.end(), ‘/‘);
              auto dir = string(i, j);//注意用法

              if (!dir.empty() && dir != ".")//当有连续的///时dir为空
              {
                  if (dir == "..")
                  {
                      if (!dirs.empty())
                          dirs.pop_back();
                  }
                  else
                      dirs.push_back(dir);
              }
              i = j;
          }

          stringstream out;
          if (dirs.empty())
          {
              out << "/";
          }
          else
          {
              for (auto dir : dirs)
              {
                  out << ‘/‘ << dir;
              }
          }

          return out.str();
      }

时间: 2024-11-06 09:48:19

Leetcode 之Simplify Path(36)的相关文章

【LeetCode】Simplify Path

Simplify Path Given an absolute path for a file (Unix-style), simplify it. Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

leetcode 71 Simplify Path

题目连接 https://leetcode.com/problems/simplify-path/ Simplify Path Description Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

leetCode 71.Simplify Path(化简路径) 解题思路和方法

Simplify Path 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

[C++]LeetCode: 117 Simplify Path (简化Unix路径 list双向链表)

题目: 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 w

[LeetCode][Java] Simplify Path

题目: 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 w

LeetCode[Stack]: Simplify Path

Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" 这个题目比较简单,用很常规的方法解决即可,测试的时候遇到什么问题再解决就行了.我的C++代码如下: string simplifyPath(str

Leetcode 之Simplify Path @ python

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" 使用一个栈来解决问题.遇到'..'弹栈,遇到'.'不操作,其他情况下压栈. 代码一: class Solution: # @param path, a

leetcode 71 Simplify Path ------ java

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 p

leetcode No71. Simplify Path

Question: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" Corner Cases: Did you consider the case where path = "/../