leetcode刷题,总结,记录,备忘 205

来源:互联网 发布:不同域名对应同一个ip 编辑:程序博客网 时间:2024/05/29 04:34

leetcode205Isomorphic Strings

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.

一个字符串中的一个字母对应于另一个字符串中相同位置的字母,做两两映射,用2个127大小的数组来表达每个字符所对应的字符,全部初始化为0,在第一次映射的时候赋值,之后就可以做两两判断了。

class Solution {public:    bool isIsomorphic(string s, string t) {      vector<char> vs(127, 0);      vector<char> vt(127, 0);            for (int i = 0; i < s.size(); ++i)      {          if (vs[s[i]] == 0)          {              vs[s[i]] = t[i];          }          if (vt[t[i]] == 0)          {              vt[t[i]] = s[i];          }          if (vs[s[i]] != t[i] || vt[t[i]] != s[i])          {              return false;          }      }            return true;    }};



0 0
原创粉丝点击