多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > leetcode 205 Isomorphic Strings

leetcode 205 Isomorphic Strings

來源:程序員人生   發布時間:2015-05-19 08:08:54 閱讀次數:2809次

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.

這里寫圖片描述

我的解決方案:

// isIsomorphic.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<map> #include<string> #include<iostream> #include<unordered_map> using namespace std; bool isIsomorphic(string s, string t) { if(s.length()!=t.length())return false; int s_length = s.length(); int t_length = t.length(); unordered_map<char,char> stemp; unordered_map<char,char> ttemp; for(int i = 0;i < s_length; i++) { if(stemp.find(s[i]) == stemp.end() && ttemp.find(t[i]) == ttemp.end()) { stemp[s[i]] = t[i]; ttemp[t[i]] = s[i]; } else { if(stemp.find(s[i]) == stemp.end() && ttemp[t[i]]!=s[i]) { return false; } else if(ttemp.find(t[i])==ttemp.end() && stemp[s[i]]!=t[i]) { return false; } else if(stemp[s[i]] != t[i] && ttemp[t[i]] != s[i]) { return false; } } } } // //pair<map<char,int>::iterator,bool> Insert_Pair; //Insert_Pair = mapString.insert(map<char,int>::value_type(s[i],(int)(s[i] - t[i]))); int _tmain(int argc, _TCHAR* argv[]) { string s = "ab"; string t = "aa"; isIsomorphic(s,t); return 0; }

unordered_map 簡介:
http://blog.csdn.net/gamecreating/article/details/7698719
http://blog.csdn.net/orzlzro/article/details/7099231
http://blog.csdn.net/sws9999/article/details/3081478

unordered_map,它與map的區分就是map是依照operator<比較判斷元素是不是相同,和比較元素的大小,然后選擇適合的位置插入到樹中。所以,如果對map進行遍歷(中序遍歷)的話,輸出的結果是有序的。順序就是依照operator< 定義的大小排序。而unordered_map是計算元素的Hash值,根據Hash值判斷元素是不是相同。所以,對unordered_map進行遍歷,結果是無序的。而hash則是把數據的存儲和查找消耗的時間大大下降;而代價僅僅是消耗比較多的內存。雖然在當前可利用內存愈來愈多的情況下,用空間換時間的做法是值得的。
用法的區分就是map的key需要定義operator<。而unordered_map需要定義hash_value函數并且重載operator==。對自定義的類型做key,就需要自己重載operator< 或hash_value()了。

python 的解決方案:

def isIsomorphic(self, s, t): if len(s) != len(t): return False def halfIsom(s, t): res = {} for i in xrange(len(s)): if s[i] not in res: res[s[i]] = t[i] elif res[s[i]] != t[i]: return False return True return halfIsom(s, t) and halfIsom(t, s)
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 精品一区二区三区免费站 | 日本亚州视频在线八a | 美国一级毛片oo | 亚洲国产成人久久一区二区三区 | 国产精品va在线观看手机版 | 一二三四视频在线观看免费高清 | 欧美日韩国产高清 | 免费xx视频 | 亚洲第一视频在线观看 | 视频一区二区中文字幕 | 五月天伊人 | 欧美日韩亚洲国产精品 | 久久精品一 | 91精品国产色综合久久不 | 日本大臿亚洲香蕉大片 | 亚洲国产成人资源在线软件 | 亚洲小说春色综合另类小说 | free性欧美tube视频 | 亚洲春色另类 | 伊人365影院| 成人区在线观看免费视频 | 在线视频亚洲欧美 | 最近的最新的中文字幕在线 | 自拍偷拍视频网 | 欧美精品一区二区三区四区 | 一区二区三区四区视频 | 中文字幕在线视频网 | 国产欧美综合精品一区二区 | 中文字幕 视频一区 | 国产欧美久久久精品影院 | 亚洲欧美日韩在线精品一区二区 | 日本一区二区三区在线 视频 | 日本欧美视频 | 亚洲午夜在线观看 | 欧美精品专区第1页 | 欧美一级乱妇老太婆特黄 | 国产成人毛片毛片久久网 | 亚洲欧美日韩中文字幕一区二区三区 | 亚洲国产成人精品青青草原100 | 国产精品秋霞午夜 | 国产精品三级视频 |