Isomorphic Strings
来源:互联网 发布:dblp数据集 编辑:程序博客网 时间:2024/06/03 23:39
题目详情:https://leetcode.com/problems/isomorphic-strings/description/
自己写的代码:
# -*- coding:utf-8 -*-class Solution(object): def isIsomorphic(self, s, t): """ :type s: str :type t: str :rtype: bool """ if len(set(s))!=len(set(t)): #如果拥有的元素就不一样的话 return False #直接返回 length=len(s) di={} #建立一个dict,其中s[i]为键,t[i]为值 i=0 #下标从0开始 while i<length: #print i if di.get(s[i],-1)==-1: #如果不存在s[i]->t[i] di[s[i]]=t[i] #则建立该种映射关系 else: #已经存在某种映射的关系 tt=di.get(s[i]) #取出该值 if tt==t[i]: #该值如果和t[i]相等 pass#则不做处理 else: #如果以前的映射值和当前的值不相等 return False i+=1 return True
看见别人写的,写了点注释
# -*- coding:utf-8 -*-class Solution(object): def isIsomorphic(self, s, t): """ :type s: str :type t: str :rtype: bool """ d = {} #检查同一个键是否映射到同一个值 #例如 s='aa' t='ab' for i in range(len(t)): if s[i] not in d: d[s[i]] = t[i] # check if mapping is correct elif d[s[i]] != t[i]: return False # check if multiple characters mapped to same character #检查不同的键是否映射到同一个值,例如:s='ab' t='aa'。这对s,t能够通过上边的循环 temp = set([]) for k in d: if d[k] in temp: return False temp.add(d[k]) return True
阅读全文
0 0
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- Isomorphic Strings
- MySQL命令行操作
- Python3学习(7)--函数【Function】
- codevs 1022 覆盖
- css禅院
- 开源项目
- Isomorphic Strings
- PHP 处理最普通的 XML 任务
- Android开发 手机测试运行报错1
- [绍棠_Swift] Swift3.0中自定义导航栏后侧滑返回功能失效
- Android 四大组件之 Service (下)
- QT Dom读xml文件
- js之查照片看器
- websocket学习-持续更新
- 算法系列——Integer to Roman