205_IsomorphicStrings
来源:互联网 发布:aigowifidisk mac 编辑:程序博客网 时间:2024/04/29 09:21
题目:
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.
思想:
采用两个hash表记录每个字符所对应的使用的个数。通过题目可知,只需要了解个数是否相等即可,无需记录具体为多少。通过映射关系若发现某位置相应“字符个数”不相等则无法映射。
代码:
bool isIsomorphic(string s, string t) {int len = s.size();if (len!=t.size()){return false;}else if (len==0){return true;}else{char s_map[128] = { 0 };char t_map[128] = { 0 };for (int i = 0; i < len; i++){if (s_map[s[i]] != t_map[t[i]]){return false;}else{s_map[s[i]] = i+1;//保证每个字符对应的“个数”不一样t_map[t[i]] = i+1;}}return true;}}
0 0
- 205_IsomorphicStrings
- 205
- 205
- NYOJ - 205
- nyoj-205
- nyist-205
- sgu 205
- NYOJ 205
- ocp-205
- 205,UIWindow
- sgnirtscihpromosI.205
- LEETCODE 205
- leetcode 205
- Moonscape (Episode 205)
- 0xcd(205)问题
- 205 class info
- NYOJ 205 求余数
- Codeforces Round #205
- 冒泡法实现数据的从大到小排列。
- 234. Palindrome Linked List
- 一种更清晰的Android架构
- web前端html+css基础 项目实例
- Linux 容器技术史话:从 chroot 到未来
- 205_IsomorphicStrings
- 通过修改注册表调整Office的COM加载项
- STC15F2K60S2知识点随记
- Netty实现带UI客户端服务器聊天功能
- TCP打洞和UDP打洞的区别
- scanf()函数和gets()函数
- 初学安卓:安卓小游戏之2048
- 第10章 vim程序编辑器
- 在Android使用AIDL实现IPC机制