leetcode题解之Isomorphic Strings

我自己的答案如下:

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int sLength = s.length();  
        int tLength = t.length();  
        if(sLength != tLength)  
            return false;  
        unordered_map<char,char> s2t;  
        unordered_map<char,char> t2s;  
          
        for(int i = 0;i<sLength;i++)  
        {  
            
            char sChar = s[i];  
            char tChar = t[i];
            
            //sChar 作为s2t的索引,与某个tChar相对应,当然如果找不到索引可以不对应
            if(s2t.find(sChar) != s2t.end() && s2t[sChar] != tChar)  
                return false;
            //s1t这边也要对应
            if(t2s.find(tChar) != t2s.end() && t2s[tChar] != sChar)  
                return false;  
            s2t[sChar] = tChar;  
            t2s[tChar] = sChar;  
        }  
          
        return true;  
    }
};



看了别人的答案之后,感觉自己很菜很垃圾:

这道题的思路跟我的上一篇博客讲的题很类似,大家可以参考一下:http://blog.csdn.net/qq_35985044/article/details/78157358

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        bool isIsomorphic(string s, string t) {
        int m1[256] = {0}, m2[256] = {0}, n = s.size();
        for (int i = 0; i < n; ++i) {
            if (m1[s[i]] != m2[t[i]]) return false;
            m1[s[i]] = i + 1;
            m2[t[i]] = i + 1;
        }
        return true;
        
    }
};



猜你喜欢

转载自blog.csdn.net/qq_35985044/article/details/78157535
今日推荐