LeetCode 205 Isomorphic Strings

来源:互联网 发布:淘宝炫富图片大全 编辑:程序博客网 时间:2024/05/16 07:18

LeetCode 205 Isomorphic Strings

#include <string>using namespace std;class Solution {public:    bool isIsomorphic(string s, string t) {        char s_t[128] = { 0 };//串s到t的映射,最多128个,因为ascii码        char t_s[128] = { 0 };//串t到s的映射        for (int i = 0; i < s.length(); i++){            if (s_t[s[i]] == 0 && t_s[t[i]] == 0){                s_t[s[i]] = t[i];                t_s[t[i]] = s[i];            }            else if (s_t[s[i]] != t[i] || t_s[t[i]] != s[i])//同一个字母不能有多个映射,必须是一一对应的                return false;        }        return true;//12ms    }};/*class Solution {public:    bool isIsomorphic(string s, string t) {        if (s.size() != t.size())            return false;        int m1[256] = { 0 };        int m2[256] = { 0 };        for (int i = 0; i<s.size(); ++i) {            if (m1[s[i]] != m2[t[i]])                return false;            m1[s[i]] = i + 1;            m2[t[i]] = i + 1;        }        return true;    }leetcode上的一个6ms的实现方法,用数字~};*/
原创粉丝点击