LeetCode之“散列表”:Isomorphic Strings
题目要求:
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.
这题难度不大。
第一个程序(28ms):
bool isIsomorphic(string s, string t) {
unordered_map<char, char> hashMap;
int sz = s.size();
for(int i = ; i < sz; i++)
{
if(hashMap.find(s[i]) != hashMap.end())
{
if(hashMap[s[i]] != t[i])
return false;
}
else
{
unordered_map<char, char>::iterator itr = hashMap.begin();
for(; itr != hashMap.end(); itr++)
if(itr->second == t[i])
return false;
hashMap[s[i]] = t[i];
}
}
return true;
}
第二个程序(24ms):
bool isIsomorphic(string s, string t) {
unordered_map<char, char> hashMap;
unordered_map<char, char> rHashMap;
int sz = s.size();
for(int i = ; i < sz; i++)
{
if(hashMap.find(s[i]) != hashMap.end())
{
if(hashMap[s[i]] != t[i])
return false;
}
else
{
if(rHashMap.find(t[i]) != rHashMap.end())
return false;
hashMap[s[i]] = t[i];
rHashMap[t[i]] = s[i];
}
}
return true;
}
看了网上别人的做法(少掉了哈希表查找的时间损耗),只需8ms:
bool isIsomorphic(string s, string t) {
char map_s[] = { };
char map_t[] = { };
int len = s.size();
for (int i = ; i < len; ++i)
{
if (map_s[s[i]]!=map_t[t[i]]) return false;
map_s[s[i]] = i+;
map_t[t[i]] = i+;
}
return true;
}
LeetCode之“散列表”:Isomorphic Strings的更多相关文章
- LeetCode 205. 同构字符串(Isomorphic Strings)
205. 同构字符串 205. Isomorphic Strings
- LeetCode(205)Isomorphic Strings
题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ch ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- 【leetcode❤python】 205. Isomorphic Strings
#-*- coding: UTF-8 -*- #转换法class Solution(object): def isIsomorphic(self, s, t): "&qu ...
- LeetCode之“散列表”:Contains Duplicate && Contains Duplicate II
1. Contains Duplicate 题目链接 题目要求: Given an array of integers, find if the array contains any duplica ...
- LeetCode之“散列表”:Single Number
题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single ...
- LeetCode之“散列表”:Valid Sudoku
题目链接 题目要求: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boar ...
- Leetcode 两数之和 (散列表)
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- [LeetCode] Isomorphic Strings
Isomorphic Strings Total Accepted: 30898 Total Submissions: 120944 Difficulty: Easy Given two string ...
随机推荐
- Unity UGUI图文混排(七) -- 下划线
之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...
- 阿里云手动安装特定版本的nginx
想添加nginx的缓存功能, 结果1.4.6还不支持. apt-get remove nginx 374 sudo apt-key add nginx_signing.key 375 deb http ...
- win 8.1 64位彻底删除王码98
安全模式 C:\Windows\SysWOW64\ 删除:winwb98.IME和winwb98.MB 注册表: 找到王码五笔相关的项.一般在最下面的以字母E开头的文件夹.全部删除. HKEY_LOC ...
- 剑指offer-面试题7:俩个栈实现队列(c)
- Java中循环声明变量方法
Java循环声明变量 之前想这样做,但是网上一直搜索不到,下面是我的方式 项目中 // 得到需要查询外表的数量,然后分别创建缓存,插入数据多的时候如果编码在缓存里面,就不需要再去查询数据库了.key: ...
- 关于MySQL-python-1.2.3.tar.gz安装失败的解决方案
关于MySQL-python-1.2.3.tar.gz安装失败的解决方案 RHEL6.4升级到python2.7.9,然后安装 MySQL-python-1.2.3.tar.gz, 报错.解决错误之后 ...
- 深入理解Express.js
转自:http://xvfeng.me/posts/understanding-expressjs/ 英文原文更赞:http://evanhahn.com/understanding-express- ...
- Android的PopupWindow使用android学习之旅(四十三)
PopupWindow简介 PopupWindow是一个类似dialog的控件,可以接受任何的view作为下拉列表显示. 用法 代码展示 package peng.liu.test; import a ...
- iOS中 基于LBXScan库二维码扫描 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 首先声明这个二维码扫描是借助于zxing. 功能模块都完全封装好了,不过界面合你口味,直接使用就好,如果不合口味,后 ...
- Android初级教程小案例之单选框RadioGroup与复选框CheckBox
Android里面的单选框和html中的其实是一样的效果.这里用到两个控件:CheckBox和RadioGroup.直接上代码: radio.xml布局文件: <?xml version=&qu ...