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

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

Leetcode 205. Isomorphic Strings

2019-11-09 21:15:04
字體:
來源:轉載
供稿:網友

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.

s思路: 1. isomorphic表示同質,對string來說,就是結構相同,里面的字母是誰不重要,比如:abb和cdd就是isomorphic,因為形式上是一樣的。如何判斷呢? 2. 略微思考,有點沒有眉目,開始有點細微的緊張。不過還好,做得多了,有底氣了,和這些情緒都相處得很好。回到正題,兩個string同時遍歷,例如:”paper”, “title”,把p映射成t,a映射成i,第二次遇到p時,就查詢之前p映射的是多少,和現在打算映射的值是否一致。如果能通過所有一致性檢查,說明就是isomorphic的;中間任何地方不一致,就不是! 3. 代碼第一次寫的時候,默認只用一個unordered_map來存s[i]->t[i]的映射,但是對”agg”和”ggg”則不正確了,還需要存t[i]->s[i]的映射,因為只有這個映射,a和g映射成g都是合法的,但題目要求,不允許兩個字母映射成一個字母。因此需要兩個map來保存相互的映射關系最保險!

class Solution {public: bool isIsomorphic(string s, string t) { // unordered_map<char,char> mm1,mm2; int i=s.size(); for(int i=0;i<s.size();i++){ if(mm1.count(s[i])){ if(mm1[s[i]]!=t[i]) return false; }else if(mm2.count(t[i])){ if(mm2[t[i]]!=s[i]) return false; } else{ mm1[s[i]]=t[i]; mm2[t[i]]=s[i]; } } return true; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西安市| 巩义市| 平和县| 平阳县| 三明市| 双桥区| 颍上县| 伊吾县| 托克托县| 田东县| 饶平县| 班玛县| 简阳市| 兴业县| 佛学| 洛南县| 威远县| 沙洋县| 西藏| 盐城市| 丰镇市| 清原| 颍上县| 辰溪县| 桃园市| 磴口县| 隆昌县| 英吉沙县| 宣恩县| 墨脱县| 观塘区| 阆中市| 佛冈县| 永城市| 华池县| 长泰县| 民权县| 烟台市| 仙居县| 烟台市| 绥棱县|