大家好,我是毛毛。ヾ(´∀ ˋ)ノ
废话不多说开始今天的解题Day~
Given two strings s
and t
, determine if they are isomorphic.
Two strings s
and t
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.
Input: s = "egg", t = "add"
Output: true
Input: s = "foo", t = "bar"
Output: false
Input: s = "paper", t = "title"
Output: true
1 <= s.length <= 5 * 10^4
t.length == s.length
s
and t
consist of any valid ascii character.首先先简单的翻译一下题目
给两个字串,要判断对应的字有没有一对多,有的话回传false,没有的话回传true。
作法大致上是这样
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
dict = {}
for index in range(len(s)):
#print(dict)
if s[index] not in dict:
if t[index] in dict.values():
# print(t[index], ":", dict.values())
return False
dict[s[index]] = t[index]
else:
if dict[s[index]] != t[index]:
return False
if t[index] in dict.values():
return False
#print(dict)
return True
bool isIsomorphic(char * s, char * t){
int pair_s[128] = {0};
int pair_t[128] = {0};
for (int i=0 ; i<strlen(s) ; i++){
int index_s = (int)(s[i]);
int index_t = (int)(t[i]);
// printf("%d, %d\n", index_s, index_t);
if (pair_s[index_s] == 0 && pair_t[index_t] == 0){
pair_s[index_s] = index_t;
pair_t[index_t] = index_s;
// printf("%d: %d, %d: %d\n", index_s, pair_s[index_s], index_t, pair_t[index_t]);
} else {
if (pair_s[index_s] == index_t && pair_t[index_t] == index_s){
continue;
} else {
return false;
}
}
}
return true;
}
Python
C
大家明天见
接下来这几天,将会带领各位以GCP的架构的视角。 向各位阐述我们先前进行的DialogFLow F...
点击劫持 点击劫持(Clickjacking) 技术又称为 界面伪装攻击(UI redress at...
今天要开始制作购物车系统所需的VScode环境。 GO GO ~ 以下内容有参考教学影片,底下有附网...
人工智慧? 机器学习? 深度学习? 刚踏入机器学习的学生自然会对这些专有名词感到相当模糊,我们就先来...
其实RISC-V官方也有开发了一个instruction accurate等级的模拟器Spike,只...