leetcode 205 : Isomorphic Strings
2016-02-23 00:46
232 查看
问题描述:
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.
思路:
这道题思路很直接,就是作字符映射,一个字符映射成另一个字符,而且不能有两个不同的字符映射到同一个字符。代码中用replace数组来存放字符映射表,而have_mapped数组记录某字符是否已经被映射,避免被两个不同的字符映射。
代码:
class Solution { public: bool isIsomorphic(string s, string t) { char replace[150]; bool have_mapped[150]; for(int i = 0; i < 150; i++) { replace[i] = 0; have_mapped[i] = false; //avoid the situation that two characters map to the same character } for(int i = 0; i < s.size();i++) { int s_index = s[i] - 0; int t_index = t[i] - 0; if(replace[s_index] == 0) { if(have_mapped[t_index] == true) return false; //test if two characters map to the same character replace[s_index] = t[i]; have_mapped[t_index] = true; } else { if(replace[s_index] != t[i]) return false; } } return true; } };
相关文章推荐
- android 成长日记 5.关于progress控件和Alertdialog和layout学习
- 逆向分析细节总结
- [040130]命运之夜Fate Stay Night【汉化硬盘版】[带全通关CG回想完美&跳过序章初始存档&中文攻略]
- java列出指定后缀名(*.jpg)的图片
- 平衡查找树之B树
- iOS 设置TextView控件内容行间距
- impala+hue
- 下载APP显示 服务器检索信息出错 ”RPC:S-7:AEC-0“等
- Remove Duplicate Letters
- 关于苹果开发的知识总结
- HTML一个form表单中有两个(多个)submit,后台如何区分(纯HTML实现,无需javascript)
- Lua学习Day02
- JVM学习(3)——总结Java内存模型
- 016-Storm借助tick消息定时器统计统计周期性业务
- 【慕课笔记】第四章 JAVA中的集合框架(上) 第6节 学生选课—课程查询
- windows7_64+vs2013_32位配置PCL_1.6.0
- Codeforces 620D Lipshitz Sequence RMQ+二分
- 3.7 代码注释和编码规范
- form提交前验证的几种方法
- nodejs爬取页面内容(utf8编码)