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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 一.題目描述 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself. For example,? Given “egg”, “add”, return true. Given “foo”, “bar”, return false. Given “paper”, “title”, return true. Note:? You may assume both s and t have the same length. ## 二.題目分析 這道題主要是建立兩個字符串中不同字符的對應關系,由于ASCII編碼的字符只有256個,而且string中是不保存null字符的,因此,可以建立兩個大小為256的字符數組來保存兩個string中每一個字符在另外一個string中的對應的關系,然后遍歷string中的每一個字符,如果相同位置的字符是互相對應的話,就處理下一個字符,如果不是互相對應的話,就在說明這兩個string不是同等結構的。 也可以通過兩個map來實現,但是map的查找過程時間復雜度為O(lgn),但是上面對于string中的每一個字符串都需要查找,因此,使用map的話,時間復雜度太高了。也可以使用hash表來做,也就是使用unordered_map來實現,但是由于ASCII編碼的字符的個數是固定的而且個數比較少,使用數組完全可以很好地實現。 ## 三.示例代碼 ~~~ class Solution { public: bool isIsomorphic(string s, string t) { vector<unsigned char> First(256, 0); // 創建一個含有256個0拷貝的vector vector<unsigned char> Second(256, 0); for (size_t index = 0; index < s.size(); ++index) { unsigned char charOfFirst = s[index]; unsigned char charOfSecond = t[index]; unsigned char& First2Second = First[charOfFirst]; unsigned char& Second2First = Second[charOfSecond]; if (First2Second == 0 && Second2First == 0) { First2Second = charOfFirst; Second2First = charOfSecond; continue; } if (First2Second != 0 && Second2First != 0) { if (First2Second != charOfFirst && Second2First != charOfSecond) return false; continue; } return false; } return true; } }; ~~~ 示例結果: ![](https://box.kancloud.cn/2016-01-05_568bb5eeedbdf.jpg) ![](https://box.kancloud.cn/2016-01-05_568bb5ef0ce41.jpg) ## 四.小結 這道題就是尋找一個好的數據結構來保存兩個string之間的字符的對應關系,根據這道題的假設,選擇數組是一個比較的解決方案。
                  <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>

                              哎呀哎呀视频在线观看