国产探花免费观看_亚洲丰满少妇自慰呻吟_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; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 色达县| 丰县| 平果县| 迁安市| 天门市| 濉溪县| 格尔木市| 西林县| 临武县| 永春县| 南昌县| 宿州市| 宝坻区| 美姑县| 泰州市| 温宿县| 山阳县| 扎囊县| 临泉县| 汝州市| 秦皇岛市| 林周县| 定州市| 吕梁市| 阳春市| 正蓝旗| 社旗县| 盈江县| 白银市| 曲阳县| 扎兰屯市| 温宿县| 东平县| 瑞丽市| 广德县| 澎湖县| 惠水县| 灌阳县| 渝北区| 察雅县| 阿尔山市|