首页 > 其他 > 详细

Leetcode 之Simplify Path(36)

时间:2016-05-26 10:08:57      阅读:191      评论:0      收藏:0      [点我收藏+]

技术分享

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

技术分享
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();
      }
View Code

 

Leetcode 之Simplify Path(36)

原文:http://www.cnblogs.com/573177885qq/p/5529729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!