<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 一.題目描述 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 = “/../”? 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”. ## 二.題目分析 簡化一個Unix風格下的文件路徑。例如輸入代表路徑的字符串:”/home/”,簡化后為:”/home”;或輸入”/a/./b/../../c/”,簡化為:”/c” 字符串中,應該了解到”..”是返回到路徑的上級目錄(如果當前根目錄則不處理),這里使用棧來記錄路徑名。在處理字符串的過程中,有以下條件: ~~~ 1\. 重復連續出現的'/',只按1個處理,即跳過重復連續出現的'/'; 2\. 如果路徑名不為"."和"..",將記錄的字符串入棧; 3\. 如果路徑名是".."且棧不為空,則需要出棧,否則無需處理; ~~~ 在遍歷字符串后,逐個取出棧中元素(即已保存的路徑名),用’/’分隔并拼接起來,注意取出的元素是從后往前連接的。 ## 三.實例代碼 ~~~ #include <iostream> #include <stack> #include <string> using namespace std; class Solution { public: string simplifyPath(string path) { stack<string> str; for (size_t i = 0; i < path.size(); ++i) { string name = ""; while (i < path.size() && path[i] == '/') ++i; // 該操作跳過斜線'/' while (i < path.size() && path[i] != '/') name += path[i++]; // 開始記錄路徑名,包括".." if (name != "." && name != "..") // 對對不同的文件名進行不同操作 str.push(name); // 非".."文件名,壓棧 else if (!str.empty() && name == "..") str.pop(); // 當前文件名為"..",表示退到上層目錄,需彈出棧 } if (str.empty()) return "/"; string result = ""; while (!str.empty()) { result = "/" + str.top() + result; // 最后組合path str.pop(); } return result; } }; ~~~ ![](https://box.kancloud.cn/2016-01-05_568bb5ee4aabb.jpg) ## 四.小結 這道題使用了string類的一些操作,該類的一些功能確實非常強大。以下是關于string類的一些用法總結:? [http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html](http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看