国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

205. Isomorphic Strings

2019-11-08 02:19:24
字體:
來源:轉載
供稿:網友

題目

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.

Subscribe to see which companies asked this question.


思路

搞兩個hashmap,分別做對應char到數字的映射,數字從1開始,再遍歷兩個string判斷


代碼

class Solution {public: bool isIsomorphic(string s, string t) { //思路,搞兩個hashmap,分別做對應char到數字的映射,數字從1開始 map<char,int> mapS; map<char,int> mapT; int numS = 1; int numT = 1; if(s.size() != t.size()) { return false; } for(size_t i = 0;i < s.size();i++) { //map中都沒找到 if(mapS.find(s.at(i)) == mapS.end() && mapT.find(t.at(i)) == mapT.end()) { mapS[s[i]] = numS; numS++; mapT[t[i]] = numT; numT++; continue; } //有一個找不到,返回false if(mapS.find(s.at(i)) == mapS.end() || mapT.find(t.at(i)) == mapT.end()) { return false; } //都找到了 if(mapS[s[i]] != mapT[t[i]]) { return false; } } return true; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澜沧| 兖州市| 富宁县| 资兴市| 兖州市| 湖南省| 噶尔县| 麻栗坡县| 金山区| 延庆县| 通江县| 黎川县| 洪泽县| 惠水县| 云林县| 宁阳县| 阿拉善左旗| 红桥区| 苏尼特右旗| 河曲县| 辽阳市| 河西区| 拉萨市| 双江| 隆尧县| 和平区| 德令哈市| 文登市| 高唐县| 武邑县| 张家川| 民权县| 嘉义市| 同德县| 彰武县| 读书| 桑日县| 龙山县| 巨野县| 从江县| 望江县|