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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Leetcode 205. Isomorphic Strings

2019-11-09 20:16:22
字體:
供稿:網(wǎng)友

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表示同質(zhì),對string來說,就是結(jié)構(gòu)相同,里面的字母是誰不重要,比如:abb和cdd就是isomorphic,因?yàn)樾问缴鲜且粯拥摹H绾闻袛嗄兀?2. 略微思考,有點(diǎn)沒有眉目,開始有點(diǎn)細(xì)微的緊張。不過還好,做得多了,有底氣了,和這些情緒都相處得很好。回到正題,兩個string同時遍歷,例如:”paper”, “title”,把p映射成t,a映射成i,第二次遇到p時,就查詢之前p映射的是多少,和現(xiàn)在打算映射的值是否一致。如果能通過所有一致性檢查,說明就是isomorphic的;中間任何地方不一致,就不是! 3. 代碼第一次寫的時候,默認(rèn)只用一個unordered_map來存s[i]->t[i]的映射,但是對”agg”和”ggg”則不正確了,還需要存t[i]->s[i]的映射,因?yàn)橹挥羞@個映射,a和g映射成g都是合法的,但題目要求,不允許兩個字母映射成一個字母。因此需要兩個map來保存相互的映射關(guān)系最保險!

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; }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 满城县| 咸丰县| 电白县| 化隆| 湖州市| 万年县| 刚察县| 宝兴县| 阳春市| 上饶市| 凤翔县| 方正县| 蒙山县| 若羌县| 桃园县| 徐汇区| 阜南县| 子洲县| 红原县| 绩溪县| 故城县| 青田县| 新野县| 大新县| 宁明县| 唐山市| 通海县| 定远县| 越西县| 福建省| 玉林市| 岳普湖县| 名山县| 阿勒泰市| 东阳市| 盘山县| 寿阳县| 小金县| 台山市| 伊春市| 府谷县|